Skip to content

Commit 3c7baa4

Browse files
committed
chore: include toHaveTitle test on main + increase coverage %
Add toHaveTitle on main so it will be easier to "compare" with multi-remote support changes
1 parent 043ba68 commit 3c7baa4

File tree

2 files changed

+206
-3
lines changed

2 files changed

+206
-3
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
import { vi, test, expect, describe, beforeEach } from 'vitest'
2+
import { browser } from '@wdio/globals'
3+
import { toHaveTitle } from '../../../src/matchers/browser/toHaveTitle'
4+
5+
const beforeAssertion = vi.fn()
6+
const afterAssertion = vi.fn()
7+
8+
const browserA = { getTitle: vi.fn().mockResolvedValue('browserA Title') } as unknown as WebdriverIO.Browser
9+
const browserB = { getTitle: vi.fn().mockResolvedValue('browserB Title') } as unknown as WebdriverIO.Browser
10+
const multiRemoteBrowserInstances: Record<string, WebdriverIO.Browser> = {
11+
'browserA': browserA,
12+
'browserB': browserB,
13+
}
14+
15+
vi.mock('@wdio/globals', () => ({
16+
browser: {
17+
getTitle: vi.fn().mockResolvedValue(''),
18+
},
19+
multiremotebrowser: {
20+
isMultiremote: true,
21+
getTitle: vi.fn().mockResolvedValue(['']),
22+
instances: ['browserA'],
23+
getInstance: (name: string) => {
24+
const instance = multiRemoteBrowserInstances[name]
25+
if (!instance) {
26+
throw new Error(`No such instance: ${name}`)
27+
}
28+
return instance
29+
}
30+
}
31+
}))
32+
33+
describe('toHaveTitle', async () => {
34+
describe('given isNot false', async () => {
35+
const defaultContext = { isNot: false, toHaveTitle }
36+
const goodTitle = 'some Title text'
37+
const wrongTitle = 'some Wrong Title text'
38+
39+
beforeEach(async () => {
40+
beforeAssertion.mockClear()
41+
afterAssertion.mockClear()
42+
})
43+
44+
describe('Browser', async () => {
45+
beforeEach(async () => {
46+
browser.getTitle = vi.fn().mockResolvedValue(goodTitle)
47+
})
48+
49+
describe('given default usage', async () => {
50+
test('when success', async () => {
51+
const result = await defaultContext.toHaveTitle(browser, goodTitle)
52+
53+
expect(result.pass).toBe(true)
54+
})
55+
56+
test('when failure', async () => {
57+
browser.getTitle = vi.fn().mockResolvedValue(wrongTitle)
58+
const result = await defaultContext.toHaveTitle(browser, goodTitle)
59+
60+
expect(result.pass).toBe(false)
61+
expect(result.message()).toEqual(`Expect window to have title
62+
63+
Expected: "some Title text"
64+
Received: "some Wrong Title text"`
65+
)
66+
})
67+
})
68+
69+
describe('given before/after assertion hooks and options', async () => {
70+
const options = {
71+
ignoreCase: true,
72+
beforeAssertion,
73+
afterAssertion,
74+
} satisfies ExpectWebdriverIO.StringOptions
75+
76+
test('when success', async () => {
77+
const result = await defaultContext.toHaveTitle(browser, goodTitle, options)
78+
79+
expect(result.pass).toBe(true)
80+
expect(beforeAssertion).toBeCalledWith({
81+
matcherName: 'toHaveTitle',
82+
expectedValue: goodTitle,
83+
options,
84+
})
85+
expect(afterAssertion).toBeCalledWith({
86+
matcherName: 'toHaveTitle',
87+
expectedValue: goodTitle,
88+
options,
89+
result,
90+
})
91+
})
92+
93+
test('when failure', async () => {
94+
browser.getTitle = vi.fn().mockResolvedValue(wrongTitle)
95+
const result = await defaultContext.toHaveTitle(browser, goodTitle, options)
96+
97+
expect(result.pass).toBe(false)
98+
expect(result.message()).toEqual(`Expect window to have title
99+
100+
Expected: "some Title text"
101+
Received: "some Wrong Title text"`
102+
)
103+
expect(beforeAssertion).toBeCalledWith({
104+
matcherName: 'toHaveTitle',
105+
expectedValue: goodTitle,
106+
options: { ignoreCase: true, beforeAssertion, afterAssertion },
107+
})
108+
expect(afterAssertion).toBeCalledWith({
109+
matcherName: 'toHaveTitle',
110+
expectedValue: goodTitle,
111+
options: { ignoreCase: true, beforeAssertion, afterAssertion },
112+
result,
113+
})
114+
})
115+
})
116+
})
117+
})
118+
119+
describe('given isNot true', async () => {
120+
const defaultContext = { isNot: true, toHaveTitle }
121+
const aTitle = 'some Title text'
122+
const negatedExpectedTitle = 'some Title text not expected to be'
123+
124+
beforeEach(async () => {
125+
beforeAssertion.mockClear()
126+
afterAssertion.mockClear()
127+
})
128+
129+
describe('Browser', async () => {
130+
beforeEach(async () => {
131+
browser.getTitle = vi.fn().mockResolvedValue(aTitle)
132+
})
133+
134+
describe('given default usage', async () => {
135+
test('when success', async () => {
136+
const result = await defaultContext.toHaveTitle(browser, negatedExpectedTitle)
137+
138+
expect(result.pass).toBe(true)
139+
})
140+
141+
test('when failure', async () => {
142+
browser.getTitle = vi.fn().mockResolvedValue(negatedExpectedTitle)
143+
const result = await defaultContext.toHaveTitle(browser, negatedExpectedTitle)
144+
145+
expect(result.pass).toBe(false)
146+
expect(result.message()).toEqual(`Expect window not to have title
147+
148+
Expected [not]: "some Title text not expected to be"
149+
Received : "some Title text not expected to be"`
150+
)
151+
})
152+
})
153+
154+
describe('given before/after assertion hooks and options', async () => {
155+
const options = {
156+
ignoreCase: true,
157+
beforeAssertion,
158+
afterAssertion,
159+
} satisfies ExpectWebdriverIO.StringOptions
160+
161+
test('when success', async () => {
162+
const result = await defaultContext.toHaveTitle(browser, negatedExpectedTitle, options)
163+
164+
expect(result.pass).toBe(true)
165+
expect(beforeAssertion).toBeCalledWith({
166+
matcherName: 'toHaveTitle',
167+
expectedValue: negatedExpectedTitle,
168+
options,
169+
})
170+
expect(afterAssertion).toBeCalledWith({
171+
matcherName: 'toHaveTitle',
172+
expectedValue: negatedExpectedTitle,
173+
options,
174+
result,
175+
})
176+
})
177+
178+
test('when failure', async () => {
179+
browser.getTitle = vi.fn().mockResolvedValue(negatedExpectedTitle)
180+
const result = await defaultContext.toHaveTitle(browser, negatedExpectedTitle, options)
181+
182+
expect(result.pass).toBe(false)
183+
expect(result.message()).toEqual(`Expect window not to have title
184+
185+
Expected [not]: "some Title text not expected to be"
186+
Received : "some Title text not expected to be"`
187+
)
188+
expect(beforeAssertion).toBeCalledWith({
189+
matcherName: 'toHaveTitle',
190+
expectedValue: negatedExpectedTitle,
191+
options: { ignoreCase: true, beforeAssertion, afterAssertion },
192+
})
193+
expect(afterAssertion).toBeCalledWith({
194+
matcherName: 'toHaveTitle',
195+
expectedValue: negatedExpectedTitle,
196+
options: { ignoreCase: true, beforeAssertion, afterAssertion },
197+
result,
198+
})
199+
})
200+
})
201+
})
202+
})
203+
})

vitest.config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export default defineConfig({
2929
'types-checks-filter-out-node_modules.js',
3030
],
3131
thresholds: {
32-
lines: 87.3,
32+
lines: 87.7,
3333
functions: 85.8,
34-
statements: 87,
35-
branches: 78.6,
34+
statements: 87.5,
35+
branches: 78.8,
3636
}
3737
}
3838
}

0 commit comments

Comments
 (0)