diff --git a/package.json b/package.json index 9d43890..0906f06 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "rgb-hex": "3.0.0" }, "devDependencies": { + "@testing-library/react": "^14.1.2", "@types/jest": "^29.5.11", "@typescript-eslint/eslint-plugin": "^6.14.0", "@typescript-eslint/parser": "^6.14.0", @@ -72,6 +73,7 @@ "eslint-plugin-prettier": "^5.0.1", "husky": "^8.0.3", "jest": "^29.7.0", + "jest-environment-jsdom": "^29.7.0", "pinst": "^3.0.0", "prettier": "^3.1.1", "ts-jest": "^29.1.1", @@ -80,8 +82,8 @@ "peerDependencies": { "@types/react": ">=17.0.0", "date-fns": ">=2.0.0", - "react": ">=17.0.0", - "preact": ">=10.0.0" + "preact": ">=10.0.0", + "react": ">=17.0.0" }, "peerDependenciesMeta": { "react": { diff --git a/test/LightningTime.test.ts b/test/LightningTime.test.ts deleted file mode 100644 index ffcbfad..0000000 --- a/test/LightningTime.test.ts +++ /dev/null @@ -1,197 +0,0 @@ -import { LightningTime } from '../src/index' - -describe('to lightning', () => { - it('should convert to lightning', () => { - const time = new Date() - time.setHours(12) - time.setMinutes(0) - time.setSeconds(0) - const lightningTime = new LightningTime() - const convert = lightningTime.convertToLightning(time) - expect(convert.lightningString).toEqual('8~0~0|0') - }) - it('should convert to lightning and get stripped charges', () => { - const time = new Date() - time.setHours(12) - time.setMinutes(0) - time.setSeconds(0) - const lightningTime = new LightningTime() - const convert = lightningTime.convertToLightning(time) - expect(convert.strippedCharges).toEqual('8~0~0') - }) - it('should convert to lightning and get colors', () => { - const time = new Date() - time.setHours(12) - time.setMinutes(0) - time.setSeconds(0) - const lightningTime = new LightningTime() - const convert = lightningTime.convertToLightning(time) - expect(convert.colors).toMatchObject({ - boltColor: '#80a100', - zapColor: '#3200d6', - sparkColor: '#f68500' - }) - }) -}) - -describe('get parts', () => { - it('should get parts', () => { - const lt = new LightningTime() - const parts = lt.getParts('8~0~0|e') - expect(parts).toMatchObject({ - bolts: '8', - zaps: '0', - sparks: '0', - charges: 'e' - }) - }) -}) - -describe('strip charges', () => { - it('should strip charges after conversion', () => { - const time = new Date() - time.setHours(12) - time.setMinutes(1) - time.setSeconds(9) - time.setMilliseconds(0) - const lt = new LightningTime() - const convert = lt.convertToLightning(time) - expect(convert.lightningString).toEqual('8~0~3|4') - const strip = convert.strippedCharges - expect(strip).toEqual('8~0~3') - }) - it('should strip charges', () => { - const lt = new LightningTime() - const strip = lt.stripCharges('8~0~0|e') - expect(strip).toEqual('8~0~0') - }) -}) - -describe('from lightning', () => { - const lightningTime = new LightningTime() - it('should convert from lightning', () => { - const convert = lightningTime.convertFromLightning('8~0~0') - - const expectedDate = new Date() - expectedDate.setHours(12) - expectedDate.setMinutes(0) - expectedDate.setSeconds(0) - expectedDate.setMilliseconds(0) - - expect(convert.getTime()).toEqual(expectedDate.getTime()) - }) - it('should convert from lightning with charges', () => { - const convert = lightningTime.convertFromLightning('8~0~0|a') - const expectedDate = new Date() - - expectedDate.setHours(12) - expectedDate.setMinutes(0) - expectedDate.setSeconds(13) - expectedDate.setMilliseconds(183) - - expect(convert.getTime()).toEqual(expectedDate.getTime()) - }) - it('should throw an error when time format is incorrect', () => { - expect(() => lightningTime.convertFromLightning('8~0|')).toThrow( - 'lightning string 8~0| is in an invalid format' - ) - }) -}) - -describe('get colors', () => { - const lightningTime = new LightningTime() - it('should get colors', () => { - const colors = lightningTime.getColors('8~1~a') - expect(colors).toMatchObject({ - boltColor: '#81a100', - zapColor: '#321ad6', - sparkColor: '#f685a0' - }) - }) - it('should get colors with charge', () => { - const colors = lightningTime.getColors('8~1~a|e') - expect(colors).toMatchObject({ - boltColor: '#81a100', - zapColor: '#321ad6', - sparkColor: '#f685ae' - }) - }) - it('should get colors with custom colors set', () => { - const lt2 = new LightningTime({ - staticBoltColors: [120, 240], - staticZapColors: [130, 130], - staticSparkColors: [50, 206] - }) - const colors = lt2.getColors('8~1~a') - expect(colors).toMatchObject({ - boltColor: '#8178f0', - zapColor: '#821a82', - sparkColor: '#32cea0' - }) - }) - it('should get colors with only some custom colors set', () => { - const lt2 = new LightningTime({ - staticBoltColors: [120, 240], - staticZapColors: [130, 130] - }) - const colors = lt2.getColors('8~1~a') - expect(colors).toMatchObject({ - boltColor: '#8178f0', - zapColor: '#821a82', - sparkColor: '#f685a0' - }) - }) - it('should throw an error when a custom time array is invalid', () => { - expect( - () => - new LightningTime({ - staticBoltColors: [120] - }) - ).toThrow('Custom colors must have a length of 2.') - - expect(() => { - new LightningTime({ - staticBoltColors: [120, 130, 140, 150] - }) - }).toThrow('Custom colors must have a length of 2.') - - expect(() => { - new LightningTime({ - staticBoltColors: [120, 300] - }) - }).toThrow('Color values must be integer values between 0 and 255 (RGB).') - - expect(() => { - new LightningTime({ - staticBoltColors: [120.5, 130.5] - }) - }).toThrow('Color values must be integer values between 0 and 255 (RGB).') - }) - it('should throw an error when time format is incorrect', () => { - expect(() => lightningTime.getColors('8~0|')).toThrow( - 'lightning string 8~0| is in an invalid format' - ) - }) -}) - -describe('set colors', () => { - it('should set custom colors after initialization', () => { - const lt = new LightningTime() - const colors = lt.getColors('8~1~a') - expect(colors).toMatchObject({ - boltColor: '#81a100', - zapColor: '#321ad6', - sparkColor: '#f685a0' - }) - - lt.setStaticColors({ - staticBoltColors: [120, 240] - }) - const colors2 = lt.getColors('8~1~a') - expect(colors2).toMatchObject({ - boltColor: '#8178f0', - zapColor: '#321ad6', - sparkColor: '#f685a0' - }) - }) -}) diff --git a/test/core.test.ts b/test/core.test.ts new file mode 100644 index 0000000..7c1489c --- /dev/null +++ b/test/core.test.ts @@ -0,0 +1,199 @@ +import { LightningTime } from '../src/index' + +describe('core library', () => { + describe('to lightning', () => { + it('should convert to lightning', () => { + const time = new Date() + time.setHours(12) + time.setMinutes(0) + time.setSeconds(0) + const lightningTime = new LightningTime() + const convert = lightningTime.convertToLightning(time) + expect(convert.lightningString).toEqual('8~0~0|0') + }) + it('should convert to lightning and get stripped charges', () => { + const time = new Date() + time.setHours(12) + time.setMinutes(0) + time.setSeconds(0) + const lightningTime = new LightningTime() + const convert = lightningTime.convertToLightning(time) + expect(convert.strippedCharges).toEqual('8~0~0') + }) + it('should convert to lightning and get colors', () => { + const time = new Date() + time.setHours(12) + time.setMinutes(0) + time.setSeconds(0) + const lightningTime = new LightningTime() + const convert = lightningTime.convertToLightning(time) + expect(convert.colors).toMatchObject({ + boltColor: '#80a100', + zapColor: '#3200d6', + sparkColor: '#f68500' + }) + }) + }) + + describe('get parts', () => { + it('should get parts', () => { + const lt = new LightningTime() + const parts = lt.getParts('8~0~0|e') + expect(parts).toMatchObject({ + bolts: '8', + zaps: '0', + sparks: '0', + charges: 'e' + }) + }) + }) + + describe('strip charges', () => { + it('should strip charges after conversion', () => { + const time = new Date() + time.setHours(12) + time.setMinutes(1) + time.setSeconds(9) + time.setMilliseconds(0) + const lt = new LightningTime() + const convert = lt.convertToLightning(time) + expect(convert.lightningString).toEqual('8~0~3|4') + const strip = convert.strippedCharges + expect(strip).toEqual('8~0~3') + }) + it('should strip charges', () => { + const lt = new LightningTime() + const strip = lt.stripCharges('8~0~0|e') + expect(strip).toEqual('8~0~0') + }) + }) + + describe('from lightning', () => { + const lightningTime = new LightningTime() + it('should convert from lightning', () => { + const convert = lightningTime.convertFromLightning('8~0~0') + + const expectedDate = new Date() + expectedDate.setHours(12) + expectedDate.setMinutes(0) + expectedDate.setSeconds(0) + expectedDate.setMilliseconds(0) + + expect(convert.getTime()).toEqual(expectedDate.getTime()) + }) + it('should convert from lightning with charges', () => { + const convert = lightningTime.convertFromLightning('8~0~0|a') + const expectedDate = new Date() + + expectedDate.setHours(12) + expectedDate.setMinutes(0) + expectedDate.setSeconds(13) + expectedDate.setMilliseconds(183) + + expect(convert.getTime()).toEqual(expectedDate.getTime()) + }) + it('should throw an error when time format is incorrect', () => { + expect(() => lightningTime.convertFromLightning('8~0|')).toThrow( + 'lightning string 8~0| is in an invalid format' + ) + }) + }) + + describe('get colors', () => { + const lightningTime = new LightningTime() + it('should get colors', () => { + const colors = lightningTime.getColors('8~1~a') + expect(colors).toMatchObject({ + boltColor: '#81a100', + zapColor: '#321ad6', + sparkColor: '#f685a0' + }) + }) + it('should get colors with charge', () => { + const colors = lightningTime.getColors('8~1~a|e') + expect(colors).toMatchObject({ + boltColor: '#81a100', + zapColor: '#321ad6', + sparkColor: '#f685ae' + }) + }) + it('should get colors with custom colors set', () => { + const lt2 = new LightningTime({ + staticBoltColors: [120, 240], + staticZapColors: [130, 130], + staticSparkColors: [50, 206] + }) + const colors = lt2.getColors('8~1~a') + expect(colors).toMatchObject({ + boltColor: '#8178f0', + zapColor: '#821a82', + sparkColor: '#32cea0' + }) + }) + it('should get colors with only some custom colors set', () => { + const lt2 = new LightningTime({ + staticBoltColors: [120, 240], + staticZapColors: [130, 130] + }) + const colors = lt2.getColors('8~1~a') + expect(colors).toMatchObject({ + boltColor: '#8178f0', + zapColor: '#821a82', + sparkColor: '#f685a0' + }) + }) + it('should throw an error when a custom time array is invalid', () => { + expect( + () => + new LightningTime({ + staticBoltColors: [120] + }) + ).toThrow('Custom colors must have a length of 2.') + + expect(() => { + new LightningTime({ + staticBoltColors: [120, 130, 140, 150] + }) + }).toThrow('Custom colors must have a length of 2.') + + expect(() => { + new LightningTime({ + staticBoltColors: [120, 300] + }) + }).toThrow('Color values must be integer values between 0 and 255 (RGB).') + + expect(() => { + new LightningTime({ + staticBoltColors: [120.5, 130.5] + }) + }).toThrow('Color values must be integer values between 0 and 255 (RGB).') + }) + it('should throw an error when time format is incorrect', () => { + expect(() => lightningTime.getColors('8~0|')).toThrow( + 'lightning string 8~0| is in an invalid format' + ) + }) + }) + + describe('set colors', () => { + it('should set custom colors after initialization', () => { + const lt = new LightningTime() + const colors = lt.getColors('8~1~a') + expect(colors).toMatchObject({ + boltColor: '#81a100', + zapColor: '#321ad6', + sparkColor: '#f685a0' + }) + + lt.setStaticColors({ + staticBoltColors: [120, 240] + }) + const colors2 = lt.getColors('8~1~a') + expect(colors2).toMatchObject({ + boltColor: '#8178f0', + zapColor: '#321ad6', + sparkColor: '#f685a0' + }) + }) + }) +}) diff --git a/test/react-clock/react-clock.test.tsx b/test/react-clock/react-clock.test.tsx new file mode 100644 index 0000000..c153c7c --- /dev/null +++ b/test/react-clock/react-clock.test.tsx @@ -0,0 +1,32 @@ +/** + * @jest-environment jsdom + */ + +import { renderHook } from '@testing-library/react' +import { LightningTime } from '../../src' +import { format } from 'date-fns' +import { useLightningTimeClock } from '../../src/react' + +describe('react clock', () => { + const { result } = renderHook(() => useLightningTimeClock()) + const now = new Date() + const lt = new LightningTime() + const { lightningString, colors } = lt.convertToLightning(now) + const formattedNowString = format(now, 'h:mm a') + + it('renders the hook', () => { + expect(result.current).toBeDefined() + }) + it('renders the correct lightning time string', () => { + expect(result.current.lightningString).toEqual(lightningString) + }) + it('renders the correct formatted normal time', () => { + expect(result.current.formattedNormalTime).toEqual(formattedNowString) + }) + it('renders the correct colors', () => { + const { boltColor, zapColor, sparkColor } = result.current.colors + expect(boltColor).toEqual(colors.boltColor) + expect(zapColor).toEqual(colors.zapColor) + expect(sparkColor).toEqual(colors.sparkColor) + }) +}) \ No newline at end of file