forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.integration.test.jsx
169 lines (136 loc) · 4.79 KB
/
index.integration.test.jsx
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
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import React from 'react';
import Routing from './routes';
import { reduxRender, act, waitFor, screen, within } from './test-utils';
import configureStore from './store';
import * as Actions from './modules/User/actions';
import { userResponse } from './testData/testServerResponses';
// setup for the app
const initialState = window.__INITIAL_STATE__;
const store = configureStore(initialState);
// need to mock this file or it'll throw ERRCONNECTED
jest.mock('./i18n');
// setup for the msw fake server
const server = setupServer(
rest.get('/session', (req, res, ctx) =>
// console.log("called server get session");
res(ctx.json(userResponse))
)
);
beforeAll(() =>
server.listen({
onUnhandledRequest: 'warn'
})
);
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
// below are fixes for jsdom-specific errors
// https://stackoverflow.com/questions/57311971/error-not-implemented-window-scrollto-how-do-we-remove-this-error-from-jest-t
const noop = () => {};
Object.defineProperty(window, 'focus', { value: noop, writable: true });
// https://github.com/jsdom/jsdom/issues/3002
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = jest.fn();
range.getClientRects = () => ({
item: () => null,
length: 0,
[Symbol.iterator]: jest.fn()
});
return range;
};
// start testing
describe('index.jsx integration', () => {
// the subject under test
const subject = () => reduxRender(<Routing />, { store });
// spy on this function and wait for it to be called before making assertions
const spy = jest.spyOn(Actions, 'getUser');
window.process.env.PREVIEW_URL = 'http://localhost:8002';
beforeEach(async () => {
act(() => {
subject();
});
await waitFor(() => expect(spy).toHaveBeenCalledTimes(1));
});
afterEach(() => {
spy.mockClear();
});
it('navbar items and the dropdowns in the navbar exist', () => {
const navigation = screen.getByRole('menubar');
expect(navigation).toBeInTheDocument();
const fileButton = within(navigation).getByRole('menuitem', {
name: /^file$/i
});
expect(fileButton).toBeInTheDocument();
const newFileButton = within(navigation).getByRole('menuitem', {
name: /^new$/i
});
expect(newFileButton).toBeInTheDocument();
// save file button not shown?
// const exampleFileButton = within(navigation).getByRole('link', {name: /^examples$/i});
// expect(exampleFileButton).toBeInTheDocument();
const editButton = within(navigation).getByRole('menuitem', {
name: /^edit$/i
});
expect(editButton).toBeInTheDocument();
const sketchButton = within(navigation).getByRole('menuitem', {
name: /^sketch$/i
});
expect(sketchButton).toBeInTheDocument();
const helpButton = within(navigation).getByRole('menuitem', {
name: /^help$/i
});
expect(helpButton).toBeInTheDocument();
});
it('toolbar elements exist', () => {
const playButton = screen.getByRole('button', {
name: /play only visual sketch/i
});
expect(playButton).toBeInTheDocument();
const stopButton = screen.getByRole('button', {
name: /stop sketch/i
});
expect(stopButton).toBeInTheDocument();
const editSketchNameButton = screen.getByRole('button', {
name: /edit sketch name/i
});
expect(editSketchNameButton).toBeInTheDocument();
expect(screen.getByText('Auto-refresh')).toBeInTheDocument();
});
it('preview exists', () => {
expect(
screen.getByRole('heading', { name: /preview/i })
).toBeInTheDocument();
const preview = screen.getByTitle(/sketch preview/i);
expect(preview).toBeInTheDocument();
});
it('code editor exists', () => {
const codeeditor = screen.getByRole('article');
expect(codeeditor).toBeInTheDocument();
});
it('sidebar exists', () => {
expect(screen.getByText('Sketch Files')).toBeInTheDocument();
});
// this test doesn't make sense anymore :/
// how to fix it? could check if sketch gets sent to iframe
// via postmessage or something
// it('clicking on play updates the preview iframe with a srcdoc, stop clears it', () => {
// const playButton = screen.getByRole('button', {
// name: /play only visual sketch/i
// });
// const preview = screen.getByRole('main', { name: /sketch preview/i });
// expect(preview.getAttribute('srcdoc')).toBeFalsy();
// act(() => {
// fireEvent.click(playButton);
// });
// expect(preview.getAttribute('srcdoc')).toBeTruthy();
// const stopButton = screen.getByRole('button', {
// name: /stop sketch/i
// });
// act(() => {
// fireEvent.click(stopButton);
// });
// expect(preview.getAttribute('srcdoc')).toMatch(/(^|")\s*($|")/);
// });
});