-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.shared-spec.ts
182 lines (150 loc) · 7.26 KB
/
app.shared-spec.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { AppControlNames, AppControlsHarness, AppDemoFormHarness, AppDemoFormInputNames, AppHarness } from './app.harness';
import { expectNotToHaveThrownAnything } from './expect.function';
import { SharedSpecContext } from './shared-spec-context.model';
export function itShouldMatchInitialScreenshot(context: SharedSpecContext) {
it('should match screenshot', async () => {
expect(await context.takeScreenshot('app-0-initially')).toBeLessThan(.5);
});
}
export function testApp(context: SharedSpecContext) {
let app: AppHarness;
let controls: AppControlsHarness;
let demoForm: AppDemoFormHarness;
beforeEach(async () => {
app = context.app;
controls = await app.getControls();
demoForm = await app.getDemoFrom();
});
it('should start', () => expect(app).toBeTruthy());
describe('initially', () => {
itShouldMatchInitialScreenshot(context);
describe('controls', () => {
it('should find all controls', async () => {
await controls.getAll();
expectNotToHaveThrownAnything();
});
it('should initially have no active element', async () => expect(await context.getActiveElementId()).toBe(''));
itShouldShow('tab');
itShouldNotShow('shift tab');
itShouldShow('backspace');
itShouldNotShow('delete');
});
describe('demoForm', () => {
it('should show first input', async () => {
expect(await demoForm.getControl('first input')).toBeTruthy();
});
it('should show second input', async () => {
expect(await demoForm.getControl('second input')).toBeTruthy();
});
it('should show textarea', async () => {
const textarea = await demoForm.getControl('textarea');
expect(textarea).toBeTruthy('not found');
expect(await textarea.matchesSelector('textarea')).toBe(true, 'is not textarea');
});
it('should show disabled input', async () => {
const disabledInput = await demoForm.getControl('disabled input');
expect(disabledInput).toBeTruthy('not found');
expect(await disabledInput.getProperty('disabled')).toBe(true, 'is not disabled');
});
it('should show input that prevents defaults', async () => {
expect(await demoForm.getControl('prevent default')).toBeTruthy();
});
it('should show button', async () => {
expect(await demoForm.getControl('button')).toBeTruthy();
});
});
});
describe('interaction', () => {
describeAfterHover('shift', () => {
it('should find alternative controls', async () => {
const controlsFound = await controls.getAll();
await context.takeScreenshot('app-1-alternative-controls');
// tslint:disable: no-string-literal
expect(controlsFound['tab']).toBeFalsy('tab');
expect(controlsFound['shift tab']).toBeTruthy('shift tab');
expect(controlsFound['backspace']).toBeFalsy('backspace');
expect(controlsFound['delete']).toBeTruthy('delete');
// tslint:enable: no-string-literal
});
});
describeClickInto('first input', () => {
describeAndHover('tab', () => itShouldHaveFocused('second input'));
describeAndHover(['shift', 'shift tab'], () => itShouldHaveFocused('button'));
describeAndHover('write "a"', () => itShouldHaveSetValueOf('first input').to('a'));
describeAndHover('write "b"', () => itShouldHaveSetValueOf('first input').to('b'));
describeAndHover('write "c"', () => itShouldHaveSetValueOf('first input').to('c'));
describeAndHover('write "Leeroy"', () => {
itShouldHaveSetValueOf('first input').to('Leeroy');
describeAndHover('backspace', () => {
itShouldHaveSetValueOf('first input').to('Leero');
});
describeAndHover(['arrow left', 'write "a"'], () => itShouldHaveSetValueOf('first input').to('Leeroay'));
describeAndHover(['arrow left', 'arrow right', 'write "a"'], () => itShouldHaveSetValueOf('first input').to('Leeroya'));
describeAndHover(['arrow left', 'shift', 'arrow right', 'write "b"'], () => itShouldHaveSetValueOf('first input').to('LeeroB'));
describeAndHover(
['arrow left', 'arrow left', 'arrow left', 'shift', 'arrow down', 'delete'],
() => itShouldHaveSetValueOf('first input').to('Lee'),
);
describeAndHover(
['arrow left', 'arrow left', 'arrow down', 'write "a"'],
() => itShouldHaveSetValueOf('first input').to('Leeroya'),
);
describeAndHover(['arrow up', 'write "a"'], () => itShouldHaveSetValueOf('first input').to('aLeeroy'));
describeAndHover(['shift', 'arrow left', 'write "c"'], () => itShouldHaveSetValueOf('first input').to('LeeroC'));
describeAndHover(['shift', 'arrow up', 'write "b"'], () => itShouldHaveSetValueOf('first input').to('B'));
});
describeAndHover('write "Jenkins"', () => {
itShouldHaveSetValueOf('first input').to('Jenkins');
});
describeAndHover('write lorem ipsum', () => {
itShouldHaveSetValueOf('first input').to(/^Lorem ipsum.{100,}/);
});
});
});
function describeAfterHover(controlName: AppControlNames | AppControlNames[], specDefinitions: () => void) {
describeHover('after', controlName, specDefinitions);
}
function describeHover(conjuction: string, controlName: AppControlNames | AppControlNames[], specDefinitions: () => void) {
const controlsToHover = typeof controlName === 'string' ? [controlName] : controlName;
describe(`${conjuction} hover "${controlsToHover.join('", "')}" button`, () => {
beforeEach(() => controlsToHover.reduce((last, next) => last.then(() => controls.hoverOver(next)), Promise.resolve()));
specDefinitions();
});
}
function describeClickInto(controlName: AppDemoFormInputNames, specDefinitions: () => void) {
describe(`click into "${controlName}"`, () => {
beforeEach(async () => await (await demoForm.getControl(controlName)).click());
specDefinitions();
});
}
function describeAndHover(controlName: AppControlNames | AppControlNames[], specDefinitions: () => void) {
describeHover('and', controlName, specDefinitions);
}
function itShouldShow(controlName: AppControlNames) {
it (`should show control "${controlName}"`, async () => expect(await controls.isShown(controlName)).toBe(true));
}
function itShouldNotShow(controlName: AppControlNames) {
it (`should *not* show control "${controlName}"`, async () => expect(await controls.isShown(controlName)).toBe(false));
}
function itShouldHaveFocused(controlName: AppDemoFormInputNames) {
it (`should have focused "${controlName}"`, async () => {
const isFocused = await (await demoForm.getControl(controlName)).isFocused();
expect(isFocused).toBe(true);
});
}
function itShouldHaveSetValueOf(controlName: AppDemoFormInputNames) {
return {
to: (expectedValue: string | RegExp) => {
it(`should have set value of "${controlName}" to "${expectedValue}"`, async () => {
const control = await demoForm.getControl(controlName);
const actualValue = await control.getProperty('value');
if (typeof expectedValue === 'string') {
expect(actualValue).toBe(expectedValue);
} else {
expect(actualValue).toMatch(expectedValue);
}
});
}
};
}
}