Skip to content

Commit 0e04210

Browse files
authored
refactor: update live-region and utils tests from Jest to Vitest (#6157)
1 parent a5c0354 commit 0e04210

File tree

9 files changed

+63
-39
lines changed

9 files changed

+63
-39
lines changed

packages/react/jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ module.exports = {
4848
'<rootDir>/src/Truncate/',
4949
'<rootDir>/src/UnderlineNav/',
5050
'<rootDir>/src/hooks/',
51+
'<rootDir>/src/live-region/',
52+
'<rootDir>/src/utils/__tests__/invariant.test.ts',
53+
'<rootDir>/src/utils/__tests__/warning.test.ts',
5154
],
5255
transformIgnorePatterns: ['node_modules/(?!@github/[a-z-]+-element|@lit-labs/react|@oddbird/popover-polyfill)'],
5356
}

packages/react/src/live-region/__tests__/Announce.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import {describe, expect, it, beforeEach, afterEach} from 'vitest'
12
import {render, screen} from '@testing-library/react'
23
import {Announce} from '../Announce'
3-
import {getLiveRegion} from '../../utils/testing'
4+
import {getLiveRegion} from './test-helpers'
45

56
describe('Announce', () => {
67
beforeEach(() => {
@@ -42,7 +43,7 @@ describe('Announce', () => {
4243
test
4344
</Announce>,
4445
)
45-
expect(screen.getByTestId('container')).toHaveStyle('color: blue')
46+
expect(screen.getByTestId('container')).toHaveStyle('color: rgb(0, 0, 255)')
4647
})
4748

4849
it('should support customizing the container element with `as`', () => {

packages/react/src/live-region/__tests__/AriaAlert.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import {describe, expect, it, beforeEach, afterEach} from 'vitest'
12
import {render, screen} from '@testing-library/react'
23
import React from 'react'
34
import {AriaAlert} from '../AriaAlert'
45
import {userEvent} from '@testing-library/user-event'
5-
import {getLiveRegion} from '../../utils/testing'
6+
import {getLiveRegion} from './test-helpers'
67

78
describe('AriaAlert', () => {
89
beforeEach(() => {
@@ -36,7 +37,7 @@ describe('AriaAlert', () => {
3637
test
3738
</AriaAlert>,
3839
)
39-
expect(screen.getByTestId('container')).toHaveStyle('color: blue')
40+
expect(screen.getByTestId('container')).toHaveStyle('color: rgb(0, 0, 255)')
4041
})
4142

4243
it('should support customizing the container element with `as`', () => {

packages/react/src/live-region/__tests__/AriaStatus.test.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import {describe, expect, it, beforeEach, afterEach} from 'vitest'
12
import {render, screen} from '@testing-library/react'
23
import React from 'react'
34
import {AriaStatus} from '../AriaStatus'
45
import {userEvent} from '@testing-library/user-event'
5-
import {getLiveRegion} from '../../utils/testing'
6+
import {getLiveRegion} from './test-helpers'
67

78
describe('AriaStatus', () => {
89
beforeEach(() => {
@@ -72,7 +73,7 @@ describe('AriaStatus', () => {
7273
test
7374
</AriaStatus>,
7475
)
75-
expect(screen.getByTestId('container')).toHaveStyle('color: blue')
76+
expect(screen.getByTestId('container')).toHaveStyle('color: rgb(0, 0, 255)')
7677
})
7778

7879
it('should support customizing the container element with `as`', () => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type {LiveRegionElement} from '@primer/live-region-element'
2+
3+
export function getLiveRegion(): LiveRegionElement {
4+
const liveRegion = document.querySelector('live-region')
5+
if (liveRegion) {
6+
return liveRegion as LiveRegionElement
7+
}
8+
throw new Error('No live-region found')
9+
}
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
import {describe, expect, test} from 'vitest'
12
import {invariant} from '../invariant'
23

3-
test('throws an error when the condition is `false`', () => {
4-
expect(() => {
5-
invariant(false, 'test')
6-
}).toThrow('test')
7-
})
4+
describe('invariant', () => {
5+
test('throws an error when the condition is `false`', () => {
6+
expect(() => {
7+
invariant(false, 'test')
8+
}).toThrow('test')
9+
})
810

9-
test('does not throw an error when the condition is `true`', () => {
10-
expect(() => {
11-
invariant(true, 'test')
12-
}).not.toThrow()
13-
})
11+
test('does not throw an error when the condition is `true`', () => {
12+
expect(() => {
13+
invariant(true, 'test')
14+
}).not.toThrow()
15+
})
1416

15-
test('formats arguments into error string', () => {
16-
expect(() => {
17-
invariant(false, 'test %s %s %s', 1, 2, 3)
18-
}).toThrow('test 1 2 3')
17+
test('formats arguments into error string', () => {
18+
expect(() => {
19+
invariant(false, 'test %s %s %s', 1, 2, 3)
20+
}).toThrow('test 1 2 3')
21+
})
1922
})
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1+
import {describe, expect, test, vi} from 'vitest'
12
import {warning} from '../warning'
23

3-
test('emits a message to console.warn() when the condition is `true`', () => {
4-
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {})
4+
describe('warning', () => {
5+
test('emits a message to console.warn() when the condition is `true`', () => {
6+
const spy = vi.spyOn(console, 'warn').mockImplementationOnce(() => {})
57

6-
warning(true, 'test')
8+
warning(true, 'test')
79

8-
expect(spy).toHaveBeenCalled()
9-
expect(spy).toHaveBeenCalledWith('Warning:', 'test')
10-
spy.mockRestore()
11-
})
10+
expect(spy).toHaveBeenCalled()
11+
expect(spy).toHaveBeenCalledWith('Warning:', 'test')
12+
spy.mockRestore()
13+
})
1214

13-
test('does not emit a message to console.warn() when the condition is `false`', () => {
14-
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {})
15+
test('does not emit a message to console.warn() when the condition is `false`', () => {
16+
const spy = vi.spyOn(console, 'warn').mockImplementationOnce(() => {})
1517

16-
warning(false, 'test')
18+
warning(false, 'test')
1719

18-
expect(spy).not.toHaveBeenCalled()
19-
spy.mockRestore()
20-
})
20+
expect(spy).not.toHaveBeenCalled()
21+
spy.mockRestore()
22+
})
2123

22-
test('formats arguments into warning string', () => {
23-
const spy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {})
24+
test('formats arguments into warning string', () => {
25+
const spy = vi.spyOn(console, 'warn').mockImplementationOnce(() => {})
2426

25-
warning(true, 'test %s %s %s', 1, 2, 3)
27+
warning(true, 'test %s %s %s', 1, 2, 3)
2628

27-
expect(spy).toHaveBeenCalled()
28-
expect(spy).toHaveBeenCalledWith('Warning:', 'test 1 2 3')
29-
spy.mockRestore()
29+
expect(spy).toHaveBeenCalled()
30+
expect(spy).toHaveBeenCalledWith('Warning:', 'test 1 2 3')
31+
spy.mockRestore()
32+
})
3033
})

packages/react/vitest.config.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export default defineConfig({
5050
'src/Truncate/**/*.test.?(c|m)[jt]s?(x)',
5151
'src/UnderlineNav/**/*.test.?(c|m)[jt]s?(x)',
5252
'src/hooks/**/*.test.?(c|m)[jt]s?(x)',
53+
'src/live-region/**/*.test.?(c|m)[jt]s?(x)',
54+
'src/utils/__tests__/invariant.test.?(c|m)[jt]s?(x)',
55+
'src/utils/__tests__/warning.test.?(c|m)[jt]s?(x)',
5356
],
5457
setupFiles: ['config/vitest/setup.ts'],
5558
css: {

script/class-name-test-status.mts

Whitespace-only changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy