Start adding test coverage

This commit is contained in:
sayhiben
2024-05-18 20:07:31 -07:00
parent 6977045f26
commit 614426b1ee
4 changed files with 107 additions and 3 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
node_modules node_modules
dist dist
coverage

View File

@@ -7,7 +7,9 @@
"build": "webpack", "build": "webpack",
"start": "npm run build -- --stats=errors-only && electron dist", "start": "npm run build -- --stats=errors-only && electron dist",
"start-local": "npm run build -- --stats=errors-only && electron dist --control.address=http://localhost:4444 --control.username=streamwall --control.password=local-dev", "start-local": "npm run build -- --stats=errors-only && electron dist --control.address=http://localhost:4444 --control.username=streamwall --control.password=local-dev",
"test": "jest" "test": "jest",
"test-unit": "jest --testPathIgnorePatterns=src/node/server.test.js",
"test-unit-coverage": "jest --coverage --testPathIgnorePatterns=src/node/server.test.js"
}, },
"author": "Max Goodhart <c@chromakode.com>", "author": "Max Goodhart <c@chromakode.com>",
"license": "MIT", "license": "MIT",

View File

@@ -1,4 +1,4 @@
import { boxesFromViewContentMap } from './geometry' import { boxesFromViewContentMap, idxInBox, idxToCoords } from './geometry'
function example([text]) { function example([text]) {
return text return text
@@ -89,3 +89,60 @@ describe.each([
expect(result).toStrictEqual(expected) expect(result).toStrictEqual(expected)
}) })
}) })
describe('idxToCoords', () => {
it('should convert index to coordinates correctly', () => {
const gridCount = 5
const idx = 12
const result = idxToCoords(gridCount, idx)
expect(result).toEqual({ x: 2, y: 2 })
})
it('should support the top-left corner', () => {
const gridCount = 5
const idx = 0
const result = idxToCoords(gridCount, idx)
expect(result).toEqual({ x: 0, y: 0 })
})
it('should support the top-right corner', () => {
const gridCount = 5
const idx = 4
const result = idxToCoords(gridCount, idx)
expect(result).toEqual({ x: 4, y: 0 })
})
it('should support the bottom-left corner', () => {
const gridCount = 5
const idx = 20
const result = idxToCoords(gridCount, idx)
expect(result).toEqual({ x: 0, y: 4 })
})
it('should support the bottom-right corner', () => {
const gridCount = 5
const idx = 24
const result = idxToCoords(gridCount, idx)
expect(result).toEqual({ x: 4, y: 4 })
})
})
describe('idxInBox', () => {
it('should return true if index is within the box', () => {
const gridCount = 5
const start = 0
const end = 24
const idx = 12
const result = idxInBox(gridCount, start, end, idx)
expect(result).toBe(true)
})
it('should return false if index is outside the box', () => {
const gridCount = 5
const start = 0
const end = 24
const idx = 25
const result = idxInBox(gridCount, start, end, idx)
expect(result).toBe(false)
})
})

44
src/web/colors.test.js Normal file
View File

@@ -0,0 +1,44 @@
import { hashText, idColor } from './colors'
import Color from 'color'
describe('colors.js tests', () => {
describe('hashText', () => {
it('should return a number within the provided range', () => {
const text = 'test'
const range = 100
const result = hashText(text, range)
expect(typeof result).toBe('number')
expect(result).toBeGreaterThanOrEqual(0)
expect(result).toBeLessThan(range)
})
})
describe('idColor', () => {
it('should return a Color object', () => {
const id = 'test'
const result = idColor(id)
expect(result).toBeInstanceOf(Color)
})
it('should return white color for empty id', () => {
const id = ''
const result = idColor(id)
expect(result.hex()).toBe('#FFFFFF')
})
it('should generate the same color for the same id', () => {
const id = 'test'
const result1 = idColor(id)
const result2 = idColor(id)
expect(result1.hex()).toBe(result2.hex())
})
it('should generate different colors for different ids', () => {
const id1 = 'test1'
const id2 = 'test2'
const result1 = idColor(id1)
const result2 = idColor(id2)
expect(result1.hex()).not.toBe(result2.hex())
})
})
})