-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpect.function.ts
106 lines (92 loc) · 4.02 KB
/
expect.function.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { TestElement } from '@angular/cdk/testing';
import { SharedSpecContext } from './shared-spec-context.model';
import { AppDemoFormHarness } from './app.harness';
async function checkSelectionRange(input: TestElement, start: number, end: number) {
const selectionStart = await input.getProperty('selectionStart');
const selectionEnd = await input.getProperty('selectionEnd');
if ((selectionStart !== start) || (selectionEnd !== end)) {
return `selection start: ${selectionStart} (${start}), selection end: ${selectionEnd} (${end})`;
} else {
return null;
}
}
async function checkSelectionDirection(input: TestElement, direction: string | RegExp) {
const selectionDirection = await input.getProperty('selectionDirection');
const directionRegExp = typeof direction === 'string' ? new RegExp('^' + direction + '$') : direction;
if (!directionRegExp.test(selectionDirection)) {
return `selection direction: ${selectionDirection} (${direction})`;
} else {
return null;
}
}
export async function assertInitialSelectionRange(input: TestElement, start: number, end: number, direction?: string) {
const selectionRangeError = await checkSelectionRange(input, start, end);
if (selectionRangeError) {
throw new Error(`given test conditions invalid! (${selectionRangeError})`);
}
if (direction) {
const selectionDirectionError = await checkSelectionDirection(input, direction);
if (selectionDirectionError) {
throw new Error(`given test conditions invalid! (${selectionDirectionError})`);
}
}
}
export async function assertInitialValue(input: TestElement, expectedValue: string) {
const actualValue = await input.getProperty('value');
if (actualValue !== expectedValue) {
throw new Error(`given test conditions invalid! Expected value "${actualValue}" to be "${expectedValue}"`);
}
}
export function expectNotToHaveThrownAnything() {
expect().nothing();
}
export async function expectSelectionRange(input: TestElement, start: number, end: number, direction?: string | RegExp) {
const selectionRangeError = await checkSelectionRange(input, start, end);
if (selectionRangeError) {
fail(selectionRangeError);
} else {
expect().nothing();
}
if (direction) {
const selectionDirectionError = await checkSelectionDirection(input, direction);
if (selectionDirectionError) {
fail(selectionDirectionError);
} else {
expect().nothing();
}
}
}
export function describeDoNothingInInputThatPreventsDefaults(
context: SharedSpecContext,
getDemoForm: () => AppDemoFormHarness,
testedAction: () => Promise<any>,
) {
describe('in input', () => {
describe('that prevents default', () => {
it('that prevents default should not move cursor nor change selection or value', async () => {
const inputThatPreventsDefault = await getDemoForm().getControl('prevent default');
await inputThatPreventsDefault.focus();
await context.setValue('12345');
await context.setSelectionRange(2, 4, 'backward');
await testedAction();
expect(await inputThatPreventsDefault.getProperty('value')).toBe('12345', 'value');
await expectSelectionRange(inputThatPreventsDefault, 2, 4, 'backward');
});
});
describe('that does not prevent default', () => {
it('should do something', async () => {
const inputThatDoesNotPreventsDefault = await getDemoForm().getControl('first input');
await inputThatDoesNotPreventsDefault.focus();
await context.setValue('12345');
await context.setSelectionRange(2, 4, 'backward');
await testedAction();
expect(false
|| (await inputThatDoesNotPreventsDefault.getProperty('value') !== '12345')
|| (await inputThatDoesNotPreventsDefault.getProperty('selectionStart') !== 2)
|| (await inputThatDoesNotPreventsDefault.getProperty('selectionEnd') !== 4)
|| (await inputThatDoesNotPreventsDefault.getProperty('selectionDirection') !== 'backward')
).toBe(true, 'did change nothing');
});
});
});
}