-
Notifications
You must be signed in to change notification settings - Fork 0
/
snappshotter.test.ts
190 lines (162 loc) · 5.46 KB
/
snappshotter.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
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
183
184
185
186
187
188
189
190
import { createSnapshotter, SnapshotterOptions } from "./snapshotter";
import { toMatchImageSnapshot } from "jest-image-snapshot";
import { Browser } from "./browser";
import { WebDriver } from "selenium-webdriver";
import { BasicHookOptions, WidthXHeightString } from "../types";
import createDebug from "debug";
import { willBeInitialized } from "./test-utils/will-be-initialized";
jest.mock("jest-image-snapshot", () => {
return {
toMatchImageSnapshot: jest.fn(),
};
});
const toMatchImageSnapshotMock = toMatchImageSnapshot as jest.MockedFunction<
typeof toMatchImageSnapshot
>;
const context = {
id: "test-story",
kind: "kind",
story: "A story",
name: "Story name",
framework: "html",
fileName: "test.js",
};
const snapshotterOptions: SnapshotterOptions = {
context,
sizes: ["1280x1024", "1024x768"],
afterEachScreenshot: jest.fn().mockReturnValue(Promise.resolve()),
beforeFirstScreenshot: jest.fn().mockReturnValue(Promise.resolve()),
beforeEachScreenshot: jest.fn().mockReturnValue(Promise.resolve()),
getMatchOptions: jest.fn().mockReturnValue({
customSnapshotsDir: "aaa",
blur: 2,
}),
};
describe("snapshotter without debugging enabled", () => {
let originalDebugScope: string = willBeInitialized<string>();
beforeEach(() => {
originalDebugScope = createDebug.disable();
});
afterEach(() => {
createDebug.enable(originalDebugScope);
});
testSnapshotter();
});
describe("snapshotter with debug enabled", () => {
let originalDebugScope: string = willBeInitialized<string>();
beforeEach(() => {
originalDebugScope = createDebug.disable();
createDebug.enable("addon-storyshots-selenium:snapshotter");
});
afterEach(() => {
createDebug.enable(originalDebugScope);
});
testSnapshotter();
});
function testSnapshotter() {
it("calls the correct browser- and hook-methods", async () => {
const { calls, trackMethodsOf } = useCallTracker();
const options: SnapshotterOptions = trackMethodsOf(snapshotterOptions);
const browser: Browser = trackMethodsOf(mockBrowser());
toMatchImageSnapshotMock.mockImplementation((...args: any[]) => {
calls.push(["toMatchImageSnapshot", ...args]);
return { pass: true, message: () => "test" };
});
const snapshotter = createSnapshotter(options);
await snapshotter.createSnapshots(browser, "http://storybook:9009");
const BASIC_HOOK_OPTIONS: BasicHookOptions = {
browserId: "chrome",
context: context,
url: "http://storybook:9009/iframe.html?id=test-story",
};
const expectedCalls = [
["prepareBrowser", "http://storybook:9009/iframe.html?id=test-story"],
["beforeFirstScreenshot", "mockWebdriver", BASIC_HOOK_OPTIONS],
["getCurrentUrl"],
["resizeTo", "1280x1024"],
["beforeEachScreenshot", "mockWebdriver", { ...BASIC_HOOK_OPTIONS, size: "1280x1024" }],
["takeScreenshot"],
[
"afterEachScreenshot",
{ ...BASIC_HOOK_OPTIONS, size: "1280x1024", image: "mockScreenshot-1280x1024" },
],
["getMatchOptions", { ...BASIC_HOOK_OPTIONS, size: "1280x1024" }],
["toMatchImageSnapshot", "mockScreenshot-1280x1024", { blur: 2, customSnapshotsDir: "aaa" }],
["getCurrentUrl"],
["resizeTo", "1024x768"],
["beforeEachScreenshot", "mockWebdriver", { ...BASIC_HOOK_OPTIONS, size: "1024x768" }],
["takeScreenshot"],
[
"afterEachScreenshot",
{ ...BASIC_HOOK_OPTIONS, size: "1024x768", image: "mockScreenshot-1024x768" },
],
["getMatchOptions", { ...BASIC_HOOK_OPTIONS, size: "1024x768" }],
["toMatchImageSnapshot", "mockScreenshot-1024x768", { blur: 2, customSnapshotsDir: "aaa" }],
];
for (let i = 0; i < expectedCalls.length; i++) {
expect(calls[i]).toEqual(expectedCalls[i]);
}
expect(calls.length).toEqual(expectedCalls.length);
});
it("collects errors", async () => {
const browser: Browser = mockBrowser();
toMatchImageSnapshotMock.mockImplementation((...args: any[]) => {
return { pass: false, message: () => "faking test failure for " + args[0] };
});
const snapshotter = createSnapshotter(snapshotterOptions);
await snapshotter.createSnapshots(browser, "http://storybook:9009");
expect(snapshotter.errors.map((error) => error.message)).toEqual([
"faking test failure for mockScreenshot-1280x1024",
"faking test failure for mockScreenshot-1024x768",
]);
});
}
type GenericFunction<ParamsType extends any[], ResultType> = (...args: ParamsType) => ResultType;
function useCallTracker() {
const calls: any[][] = [];
function trackedFunction<R, P extends any[]>(
name: string,
fn: GenericFunction<P, R>
): GenericFunction<P, R> {
return (...args: P) => {
calls.push([name, ...args]);
return fn(...args);
};
}
return {
calls,
trackMethodsOf<T>(object: T): T {
const trackedObject = { ...object };
Object.keys(object).forEach((key) => {
if (typeof object[key] === "function") {
trackedObject[key] = trackedFunction(
key,
trackedObject[key] as GenericFunction<any, any>
);
}
});
return trackedObject;
},
};
}
function mockBrowser(): Browser {
let currentUrl: string | null = null;
let currentSize: string | null = null;
return {
id: "chrome",
async prepareBrowser(url: string): Promise<void> {
currentUrl = url;
},
driver: ("mockWebdriver" as any) as WebDriver,
getCurrentUrl(): string {
return currentUrl;
},
async resizeTo(size: WidthXHeightString): Promise<void> {
currentSize = size;
},
async takeScreenshot(): Promise<Buffer> {
return (("mockScreenshot-" + currentSize) as any) as Buffer;
},
quit: jest.fn().mockReturnValue(Promise.resolve()),
};
}