-
Notifications
You must be signed in to change notification settings - Fork 827
/
jest.helpers.js
59 lines (53 loc) · 1.75 KB
/
jest.helpers.js
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
// Copyright (c) 2015-present, salesforce.com, inc. All rights reserved
// Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license
/* global jasmine */
/* eslint-env jest */
import { assertMatchesDOM } from '@salesforce-ux/instant-vrt/matcher';
import fetch from 'isomorphic-fetch';
import ReactDOM from 'react-dom/server';
import renderer from 'react-test-renderer';
import { beautify } from './shared/utils/beautify';
import { renderWithBetterError } from './shared/utils/react';
export default (directory, port) => {
let CURRENT_TEST_NAME;
jasmine.getEnv().addReporter({
specStarted: function(result) {
CURRENT_TEST_NAME = result.fullName;
}
});
return {
matchesMarkup: reactElement => {
// https://jestjs.io/docs/en/snapshot-testing#snapshot-testing-with-jest
const dom = renderer.create(reactElement).toJSON();
expect(dom).toMatchSnapshot();
},
matchesMarkupAndStyle: async element => {
const renderedMarkup =
typeof element === 'string'
? element
: renderWithBetterError(ReactDOM)(
element,
`${CURRENT_TEST_NAME} failed on ${element}`
);
const markupAndStyle = await fetch(
'http://localhost:12345/api/getMarkupAndStyle',
{
method: 'POST',
headers: {
'Content-Type': 'text/plain'
},
body: renderedMarkup
}
)
.then(res => res.json())
.then(diff => ({ html: beautify(renderedMarkup), style: diff.style }));
assertMatchesDOM({
directory,
dom: markupAndStyle,
name: CURRENT_TEST_NAME,
// Set in "jest.setup.global.js"
update: process.env.JEST_UPDATE_SNAPSHOTS === 'true'
});
}
};
};