Skip to content

Commit

Permalink
initial working test
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewStanciu committed Dec 13, 2023
1 parent c83a238 commit 050d1c6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,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",
Expand Down
16 changes: 16 additions & 0 deletions test/react-clock/clock-test-component.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react";
import { useLightningTimeClock } from "../../src/react";

export function Clock() {
const { lightningString, formattedNormalTime, colors } = useLightningTimeClock()
const { boltColor, zapColor, sparkColor } = colors
return (
<div>
<p data-testid="lightning string">{lightningString}</p>
<p data-testid="formatted normal time">{formattedNormalTime}</p>
<p data-testid="bolt color">{boltColor}</p>
<p data-testid="zap color">{zapColor}</p>
<p data-testid="spark color">{sparkColor}</p>
</div>
)
}
35 changes: 35 additions & 0 deletions test/react-clock/react-clock.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @jest-environment jsdom
*/

import {render, screen} from '@testing-library/react'
import { Clock } from './clock-test-component'
import React from 'react'
import { LightningTime } from '../../src'
import { format } from 'date-fns'

describe('react clock', () => {
it('renders a react clock', () => {
render(<Clock />)
expect(screen.getByTestId('lightning string')).toBeDefined()
expect(screen.getByTestId('formatted normal time')).toBeDefined()
expect(screen.getByTestId('bolt color')).toBeDefined()
expect(screen.getByTestId('zap color')).toBeDefined()
expect(screen.getByTestId('spark color')).toBeDefined()
})
it('renders the correct lightning time', () => {
render(<Clock />)
const now = new Date()
const lt = new LightningTime()
const { lightningString, colors } = lt.convertToLightning(now)
const formattedNowString = format(now, 'h:mm a')

// this will occasionally fail if the test is run right before a new spark
// TODO: find a less flaky way to test this
expect(screen.getByTestId('lightning string').innerHTML).toEqual(lightningString)
expect(screen.getByTestId('formatted normal time').innerHTML).toEqual(formattedNowString)
expect(screen.getByTestId('bolt color').innerHTML).toEqual(colors.boltColor)
expect(screen.getByTestId('zap color').innerHTML).toEqual(colors.zapColor)
expect(screen.getByTestId('spark color').innerHTML).toEqual(colors.sparkColor)
})
})

0 comments on commit 050d1c6

Please sign in to comment.