|
1 | 1 | import { setupServer } from 'msw/node';
|
2 | 2 | import { rest } from 'msw';
|
3 |
| -import { act, render } from '@testing-library/react'; |
4 |
| -import { userResponse } from './redux_test_stores/test_server_responses'; |
| 3 | +import { |
| 4 | + act, |
| 5 | + fireEvent, |
| 6 | + prettyDOM, |
| 7 | + render, |
| 8 | + screen, |
| 9 | + within |
| 10 | +} from '@testing-library/react'; |
| 11 | +import userResponse from './redux_test_stores/test_server_responses'; |
5 | 12 |
|
| 13 | +// need to mock this file or it'll throw ERRCONNECTED |
6 | 14 | jest.mock('./i18n');
|
7 | 15 |
|
| 16 | +// setup for the msw fake server |
8 | 17 | const server = setupServer(
|
9 |
| - rest.get('/session', (req, res, ctx) => { |
10 |
| - console.log('called'); |
11 |
| - return res(ctx.json(userResponse)); |
12 |
| - }) |
| 18 | + rest.get('/session', (req, res, ctx) => |
| 19 | + res(ctx.json(userResponse.userResponse)) |
| 20 | + ) |
13 | 21 | );
|
14 | 22 |
|
15 |
| -beforeAll(() => server.listen()); |
| 23 | +beforeAll(() => |
| 24 | + server.listen({ |
| 25 | + onUnhandledRequest: 'warn' |
| 26 | + }) |
| 27 | +); |
16 | 28 | afterEach(() => server.resetHandlers());
|
17 | 29 | afterAll(() => server.close());
|
18 | 30 |
|
19 |
| -describe('Application root', () => { |
20 |
| - // eslint-disable-next-line global-require |
21 |
| - const subject = () => require('./index'); |
| 31 | +// https://stackoverflow.com/questions/57311971/error-not-implemented-window-scrollto-how-do-we-remove-this-error-from-jest-t |
| 32 | +const noop = () => {}; |
| 33 | +Object.defineProperty(window, 'focus', { value: noop, writable: true }); |
| 34 | + |
| 35 | +// https://github.com/jsdom/jsdom/issues/3002 |
| 36 | +document.createRange = () => { |
| 37 | + const range = new Range(); |
| 38 | + |
| 39 | + range.getBoundingClientRect = jest.fn(); |
| 40 | + |
| 41 | + range.getClientRects = () => ({ |
| 42 | + item: () => null, |
| 43 | + length: 0, |
| 44 | + [Symbol.iterator]: jest.fn() |
| 45 | + }); |
| 46 | + |
| 47 | + return range; |
| 48 | +}; |
| 49 | + |
| 50 | +describe('index.jsx integration', () => { |
| 51 | + let container = null; |
| 52 | + |
| 53 | + // we only run the setup once because require only works once |
| 54 | + beforeAll(() => { |
| 55 | + // setup a DOM element as a render target |
| 56 | + container = document.createElement('div'); |
| 57 | + container.id = 'root'; |
| 58 | + document.body.appendChild(container); |
| 59 | + // eslint-disable-next-line global-require |
| 60 | + require('./index'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('navbar items and the dropdowns in the navbar exist', () => { |
| 64 | + const navigation = screen.getByRole('navigation'); |
| 65 | + expect(navigation).toBeInTheDocument(); |
| 66 | + |
| 67 | + const fileButton = within(navigation).getByRole('button', { |
| 68 | + name: /^file$/i |
| 69 | + }); |
| 70 | + expect(fileButton).toBeInTheDocument(); |
| 71 | + |
| 72 | + const newFileButton = within(navigation).getByRole('button', { |
| 73 | + name: /^new$/i |
| 74 | + }); |
| 75 | + expect(newFileButton).toBeInTheDocument(); |
| 76 | + |
| 77 | + // save file button not shown? |
| 78 | + |
| 79 | + // const exampleFileButton = within(navigation).getByRole('link', {name: /^examples$/i}); |
| 80 | + // expect(exampleFileButton).toBeInTheDocument(); |
| 81 | + |
| 82 | + const editButton = within(navigation).getByRole('button', { |
| 83 | + name: /^edit$/i |
| 84 | + }); |
| 85 | + expect(editButton).toBeInTheDocument(); |
22 | 86 |
|
23 |
| - it('should render without crashing', () => { |
24 |
| - const div = document.createElement('div'); |
25 |
| - div.id = 'root'; |
26 |
| - document.body.appendChild(div); |
| 87 | + const sketchButton = within(navigation).getByRole('button', { |
| 88 | + name: /^sketch$/i |
| 89 | + }); |
| 90 | + expect(sketchButton).toBeInTheDocument(); |
| 91 | + |
| 92 | + const helpButton = within(navigation).getByRole('button', { |
| 93 | + name: /^help$/i |
| 94 | + }); |
| 95 | + expect(helpButton).toBeInTheDocument(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('toolbar elements exist', () => { |
| 99 | + const playButton = screen.getByRole('button', { |
| 100 | + name: /play only visual sketch/i |
| 101 | + }); |
| 102 | + expect(playButton).toBeInTheDocument(); |
| 103 | + |
| 104 | + const stopButton = screen.getByRole('button', { |
| 105 | + name: /stop sketch/i |
| 106 | + }); |
| 107 | + expect(stopButton).toBeInTheDocument(); |
| 108 | + |
| 109 | + const editSketchNameButton = screen.getByRole('button', { |
| 110 | + name: /edit sketch name/i |
| 111 | + }); |
| 112 | + expect(editSketchNameButton).toBeInTheDocument(); |
| 113 | + |
| 114 | + expect(screen.getByText('Auto-refresh')).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it('preview exists', () => { |
| 118 | + expect( |
| 119 | + screen.getByRole('heading', { name: /preview/i }) |
| 120 | + ).toBeInTheDocument(); |
| 121 | + const preview = screen.getByRole('main', { name: /sketch output/i }); |
| 122 | + expect(preview).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('code editor exists', () => { |
| 126 | + const codeeditor = screen.getByRole('article'); |
| 127 | + expect(codeeditor).toBeInTheDocument(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('sidebar exists', () => { |
| 131 | + expect(screen.getByText('Sketch Files')).toBeInTheDocument(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('clicking on play updates the preview iframe with a srcdoc, stop clears it', () => { |
| 135 | + const playButton = screen.getByRole('button', { |
| 136 | + name: /play only visual sketch/i |
| 137 | + }); |
| 138 | + const preview = screen.getByRole('main', { name: /sketch output/i }); |
| 139 | + expect(preview.getAttribute('srcdoc')).toBeFalsy(); |
| 140 | + act(() => { |
| 141 | + fireEvent.click(playButton); |
| 142 | + }); |
| 143 | + |
| 144 | + expect(preview.getAttribute('srcdoc')).toBeTruthy(); |
| 145 | + |
| 146 | + const stopButton = screen.getByRole('button', { |
| 147 | + name: /stop sketch/i |
| 148 | + }); |
27 | 149 | act(() => {
|
28 |
| - subject(); |
| 150 | + fireEvent.click(stopButton); |
| 151 | + }); |
| 152 | + expect(preview.getAttribute('srcdoc')).toMatch(/(^|")\s*($|")/); |
| 153 | + }); |
| 154 | + |
| 155 | + it('clicking on a file in the sidebar changes the text content of the codemirror editor', () => { |
| 156 | + const indexHTMLButton = screen.getByRole('button', { |
| 157 | + name: 'index.html' |
29 | 158 | });
|
30 | 159 |
|
31 |
| - // expect(ReactDOM.render).toHaveBeenCalledWith(<App />, div); |
| 160 | + // expect(screen.getByText("createCanvas")).toBeInTheDocument(); |
| 161 | + const codeeditor = screen.getByRole('article'); |
| 162 | + console.log(prettyDOM(codeeditor)); |
| 163 | + expect(indexHTMLButton).toBeInTheDocument(); |
| 164 | + |
| 165 | + const startingeditorcode = codeeditor.textContent; |
| 166 | + console.log(startingeditorcode); |
| 167 | + act(() => { |
| 168 | + fireEvent.click(indexHTMLButton); |
| 169 | + }); |
| 170 | + expect(startingeditorcode).not.toBe(codeeditor.textContent); |
32 | 171 | });
|
33 | 172 | });
|
0 commit comments