-
-
Notifications
You must be signed in to change notification settings - Fork 289
/
basic.test.ts
75 lines (64 loc) · 2.81 KB
/
basic.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { expect, test } from '@playwright/test'
test.describe('basic behavior', async () => {
test.beforeEach(async ({ page }) => {
await page.goto('/')
})
test('input props are forwarded', async ({ page }) => {
const input = page.locator(`input[placeholder="Search…"]`)
await expect(input).toHaveCount(1)
})
test('item value is derived from textContent', async ({ page }) => {
const item = page.locator(`[cmdk-item][data-value="Item"]`)
await expect(item).toHaveText('Item')
})
test('item value prop is preferred over textContent', async ({ page }) => {
const item = page.locator(`[cmdk-item][data-value="xxx"]`)
await expect(item).toHaveText('Value')
})
test('item onSelect is called on click', async ({ page }) => {
const item = page.locator(`[cmdk-item][data-value="Item"]`)
await item.click()
expect(await page.evaluate(() => (window as any).onSelect)).toEqual('Item selected')
})
test('first item is selected by default', async ({ page }) => {
const item = page.locator(`[cmdk-item][aria-selected="true"]`)
await expect(item).toHaveText('Item')
})
test('first item is selected when search changes', async ({ page }) => {
const input = page.locator(`[cmdk-input]`)
await input.type('x')
const selected = page.locator(`[cmdk-item][aria-selected="true"]`)
await expect(selected).toHaveText('Value')
})
test('items filter when searching', async ({ page }) => {
const input = page.locator(`[cmdk-input]`)
await input.type('x')
const removed = page.locator(`[cmdk-item][data-value="Item"]`)
const remains = page.locator(`[cmdk-item][data-value="xxx"]`)
await expect(removed).toHaveCount(0)
await expect(remains).toHaveCount(1)
})
test('items filter when searching by keywords', async ({ page }) => {
const input = page.locator(`[cmdk-input]`)
await input.type('key')
const removed = page.locator(`[cmdk-item][data-value="xxx"]`)
const remains = page.locator(`[cmdk-item][data-value="Item"]`)
await expect(removed).toHaveCount(0)
await expect(remains).toHaveCount(1)
})
test('empty component renders when there are no results', async ({ page }) => {
const input = page.locator('[cmdk-input]')
await input.type('z')
await expect(page.locator(`[cmdk-item]`)).toHaveCount(0)
await expect(page.locator(`[cmdk-empty]`)).toHaveText('No results.')
})
test('className is applied to each part', async ({ page }) => {
await expect(page.locator(`.root`)).toHaveCount(1)
await expect(page.locator(`.input`)).toHaveCount(1)
await expect(page.locator(`.list`)).toHaveCount(1)
await expect(page.locator(`.item`)).toHaveCount(2)
await page.locator('[cmdk-input]').type('zzzz')
await expect(page.locator(`.item`)).toHaveCount(0)
await expect(page.locator(`.empty`)).toHaveCount(1)
})
})