Skip to content

Commit 3352c9b

Browse files
committed
Add tests and snapshots
1 parent 8ed2f62 commit 3352c9b

3 files changed

Lines changed: 215 additions & 0 deletions

File tree

src/Timecode.test.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import React from 'react'
2+
import { render } from '@testing-library/react'
3+
import '@testing-library/jest-dom'
4+
import { Timecode } from './Timecode'
5+
6+
describe('<Timecode />', () => {
7+
test('render markup - default component', async () => {
8+
const { container, getByText } = render(<Timecode />)
9+
10+
expect(getByText('0:00')).toBeInTheDocument()
11+
expect(container).toMatchSnapshot()
12+
})
13+
14+
test('render markup - custom component', async () => {
15+
const { container } = render(<Timecode as="p" />)
16+
17+
expect(container.firstChild.nodeName).toBe('P')
18+
})
19+
20+
test('render format - H:?m:ss (default)', async () => {
21+
const { getByText } = render(<Timecode />)
22+
23+
expect(getByText('0:00')).toBeInTheDocument()
24+
})
25+
26+
test('render format - H:?mm:ss', async () => {
27+
const { getByText } = render(<Timecode format="H:?mm:ss" />)
28+
29+
expect(getByText('00:00')).toBeInTheDocument()
30+
})
31+
32+
test('render format - HH:mm:ss.SSS', async () => {
33+
const { getByText } = render(<Timecode format="HH:mm:ss.SSS" />)
34+
35+
expect(getByText('00:00:00.000')).toBeInTheDocument()
36+
})
37+
38+
test('render format - H:mm:ss.SSS', async () => {
39+
const { getByText } = render(<Timecode format="H:mm:ss.SSS" />)
40+
41+
expect(getByText('0:00:00.000')).toBeInTheDocument()
42+
})
43+
44+
test('render format - H:?mm:ss.SSS', async () => {
45+
const { getByText } = render(<Timecode format="H:?mm:ss.SSS" />)
46+
47+
expect(getByText('00:00.000')).toBeInTheDocument()
48+
})
49+
50+
test('render format - H:?m:ss.SSS', async () => {
51+
const { getByText } = render(<Timecode format="H:?m:ss.SSS" />)
52+
53+
expect(getByText('0:00.000')).toBeInTheDocument()
54+
})
55+
56+
test('render format - HH:mm:ss', async () => {
57+
const { getByText } = render(<Timecode format="HH:mm:ss" />)
58+
59+
expect(getByText('00:00:00')).toBeInTheDocument()
60+
})
61+
62+
test('render format - H:mm:ss', async () => {
63+
const { getByText } = render(<Timecode format="H:mm:ss" />)
64+
65+
expect(getByText('0:00:00')).toBeInTheDocument()
66+
})
67+
68+
test('render format - H:?mm:ss', async () => {
69+
const { getByText } = render(<Timecode format="H:?mm:ss" />)
70+
71+
expect(getByText('00:00')).toBeInTheDocument()
72+
})
73+
74+
test('render format - H:mm', async () => {
75+
const { getByText } = render(<Timecode format="H:mm" />)
76+
77+
expect(getByText('0:00')).toBeInTheDocument()
78+
})
79+
80+
test('render format - s.SSS', async () => {
81+
const { getByText } = render(<Timecode format="s.SSS" />)
82+
83+
expect(getByText('0.000')).toBeInTheDocument()
84+
})
85+
86+
test('render format - s.SS', async () => {
87+
const { getByText } = render(<Timecode format="s.SS" />)
88+
89+
expect(getByText('0.00')).toBeInTheDocument()
90+
})
91+
92+
test('render format w/ time - H:?m:ss (default)', async () => {
93+
const { getByText } = render(<Timecode time={3600000} />)
94+
95+
expect(getByText('1:00:00')).toBeInTheDocument()
96+
})
97+
98+
test('render format w/ time - H:?mm:ss.SSS', async () => {
99+
const { getByText } = render((
100+
<Timecode
101+
format="H:?mm:ss.SSS"
102+
time={3600000}
103+
/>
104+
))
105+
106+
expect(getByText('1:00:00.000')).toBeInTheDocument()
107+
})
108+
109+
test('render format w/ time - H:?m:ss.SSS', async () => {
110+
const { getByText } = render((
111+
<Timecode
112+
format="H:?m:ss.SSS"
113+
time={3600000}
114+
/>
115+
))
116+
117+
expect(getByText('1:00:00.000')).toBeInTheDocument()
118+
})
119+
120+
test('render format w/ time - H:?mm:ss', async () => {
121+
const { getByText } = render((
122+
<Timecode
123+
format="H:?mm:ss"
124+
time={3600000}
125+
/>
126+
))
127+
128+
expect(getByText('1:00:00')).toBeInTheDocument()
129+
})
130+
131+
test('render format w/ minutes - H:?m:ss (default)', async () => {
132+
const { getByText } = render(<Timecode time={5400000} />)
133+
134+
expect(getByText('1:30:00')).toBeInTheDocument()
135+
})
136+
137+
test('render format w/ seconds - H:?m:ss (default)', async () => {
138+
const { getByText } = render(<Timecode time={5430000} />)
139+
140+
expect(getByText('1:30:30')).toBeInTheDocument()
141+
})
142+
143+
test('render prefix', async () => {
144+
const { getByText } = render(<Timecode prefix="-" />)
145+
146+
expect(getByText('-0:00')).toBeInTheDocument()
147+
})
148+
149+
test('render postfix', async () => {
150+
const { getByText } = render(<Timecode postfix="/left" />)
151+
152+
expect(getByText('0:00/left')).toBeInTheDocument()
153+
})
154+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<Timecode /> render markup - default component 1`] = `
4+
<div>
5+
<span>
6+
0:00
7+
</span>
8+
</div>
9+
`;

src/utils.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { parseTime, pad, formatMilliseconds, formatTimecode } from './utils'
2+
3+
describe('utils', () => {
4+
test('parseTime', async () => {
5+
expect(parseTime(0)).toEqual({ hours: 0, minutes: 0, seconds: 0, milliseconds: 0 })
6+
expect(parseTime(1000)).toEqual({ hours: 0, minutes: 0, seconds: 1, milliseconds: 0 })
7+
expect(parseTime(1100)).toEqual({ hours: 0, minutes: 0, seconds: 1, milliseconds: 100 })
8+
expect(parseTime(1120)).toEqual({ hours: 0, minutes: 0, seconds: 1, milliseconds: 120 })
9+
expect(parseTime(1123)).toEqual({ hours: 0, minutes: 0, seconds: 1, milliseconds: 123 })
10+
expect(parseTime(60000)).toEqual({ hours: 0, minutes: 1, seconds: 0, milliseconds: 0 })
11+
expect(parseTime(3600000)).toEqual({ hours: 1, minutes: 0, seconds: 0, milliseconds: 0 })
12+
expect(parseTime(3661000)).toEqual({ hours: 1, minutes: 1, seconds: 1, milliseconds: 0 })
13+
expect(parseTime(3661123)).toEqual({ hours: 1, minutes: 1, seconds: 1, milliseconds: 123 })
14+
})
15+
16+
test('pad', async () => {
17+
expect(pad(0)).toBe('00')
18+
expect(pad(0, 2)).toBe('00')
19+
expect(pad(0, 1)).toBe('0')
20+
expect(pad(1)).toBe('01')
21+
expect(pad(1, 2)).toBe('01')
22+
expect(pad(1, 1)).toBe('1')
23+
expect(pad(10)).toBe('10')
24+
expect(pad(10, 2)).toBe('10')
25+
expect(pad(10, 1)).toBe('10')
26+
expect(pad(100)).toBe('100')
27+
expect(pad(100, 2)).toBe('100')
28+
expect(pad(100, 1)).toBe('100')
29+
})
30+
31+
test('formatMilliseconds', async () => {
32+
expect(formatMilliseconds(0)).toBe('000')
33+
expect(formatMilliseconds(0, 2)).toBe('00')
34+
expect(formatMilliseconds(0, 1)).toBe('0')
35+
expect(formatMilliseconds(1000)).toBe('1000')
36+
expect(formatMilliseconds(1000, 2)).toBe('1000')
37+
expect(formatMilliseconds(1000, 1)).toBe('1000')
38+
})
39+
40+
test('formatTimecode', async () => {
41+
expect(formatTimecode({ format: 'H:?m:ss', time: 0 })).toBe('0:00')
42+
expect(formatTimecode({ format: 'H:?mm:ss', time: 0 })).toBe('00:00')
43+
expect(formatTimecode({ format: 'mm:ss', time: 0 })).toBe('00:00')
44+
expect(formatTimecode({ format: 'mm:ss', time: 3600000 })).toBe('60:00')
45+
expect(formatTimecode({ format: 'HH:mm:ss.SSS', time: 0 })).toBe('00:00:00.000')
46+
expect(formatTimecode({ format: 'H:mm:ss.SSS', time: 0 })).toBe('0:00:00.000')
47+
expect(formatTimecode({ format: 'H:?mm:ss.SSS', time: 0 })).toBe('00:00.000')
48+
expect(formatTimecode({ format: 'H:?m:ss.SSS', time: 0 })).toBe('0:00.000')
49+
expect(formatTimecode({ format: 'HH:mm:ss', time: 0 })).toBe('00:00:00')
50+
expect(formatTimecode({ format: 'H:mm:ss', time: 0 })).toBe('0:00:00')
51+
})
52+
})

0 commit comments

Comments
 (0)