|
| 1 | +import { setupServer } from 'msw/node'; |
| 2 | +import { rest } from 'msw'; |
| 3 | +import React from 'react'; |
| 4 | +import { Router, browserHistory } from 'react-router'; |
| 5 | + |
| 6 | +import { |
| 7 | + reduxRender, |
| 8 | + act, |
| 9 | + waitFor, |
| 10 | + fireEvent, |
| 11 | + screen, |
| 12 | + within |
| 13 | +} from './test-utils'; |
| 14 | +import configureStore from './store'; |
| 15 | +import routes from './routes'; |
| 16 | +import * as Actions from './modules/User/actions'; |
| 17 | +import { userResponse } from './testData/testServerResponses'; |
| 18 | + |
| 19 | +// setup for the app |
| 20 | +const history = browserHistory; |
| 21 | +const initialState = window.__INITIAL_STATE__; |
| 22 | +const store = configureStore(initialState); |
| 23 | + |
| 24 | +// need to mock this file or it'll throw ERRCONNECTED |
| 25 | +jest.mock('./i18n'); |
| 26 | + |
| 27 | +// setup for the msw fake server |
| 28 | +const server = setupServer( |
| 29 | + rest.get('/session', (req, res, ctx) => |
| 30 | + // console.log("called server get session"); |
| 31 | + res(ctx.json(userResponse)) |
| 32 | + ) |
| 33 | +); |
| 34 | + |
| 35 | +beforeAll(() => |
| 36 | + server.listen({ |
| 37 | + onUnhandledRequest: 'warn' |
| 38 | + }) |
| 39 | +); |
| 40 | +afterEach(() => server.resetHandlers()); |
| 41 | +afterAll(() => server.close()); |
| 42 | + |
| 43 | +// below are fixes for jsdom-specific errors |
| 44 | +// https://stackoverflow.com/questions/57311971/error-not-implemented-window-scrollto-how-do-we-remove-this-error-from-jest-t |
| 45 | +const noop = () => {}; |
| 46 | +Object.defineProperty(window, 'focus', { value: noop, writable: true }); |
| 47 | + |
| 48 | +// https://github.com/jsdom/jsdom/issues/3002 |
| 49 | +document.createRange = () => { |
| 50 | + const range = new Range(); |
| 51 | + |
| 52 | + range.getBoundingClientRect = jest.fn(); |
| 53 | + |
| 54 | + range.getClientRects = () => ({ |
| 55 | + item: () => null, |
| 56 | + length: 0, |
| 57 | + [Symbol.iterator]: jest.fn() |
| 58 | + }); |
| 59 | + |
| 60 | + return range; |
| 61 | +}; |
| 62 | + |
| 63 | +// start testing |
| 64 | +describe('index.jsx integration', () => { |
| 65 | + // the subject under test |
| 66 | + const subject = () => |
| 67 | + reduxRender(<Router history={history} routes={routes(store)} />, { store }); |
| 68 | + |
| 69 | + // spy on this function and wait for it to be called before making assertions |
| 70 | + const spy = jest.spyOn(Actions, 'getUser'); |
| 71 | + |
| 72 | + beforeEach(async () => { |
| 73 | + act(() => { |
| 74 | + subject(); |
| 75 | + }); |
| 76 | + |
| 77 | + await waitFor(() => expect(spy).toHaveBeenCalledTimes(1)); |
| 78 | + }); |
| 79 | + |
| 80 | + afterEach(() => { |
| 81 | + spy.mockClear(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('navbar items and the dropdowns in the navbar exist', () => { |
| 85 | + const navigation = screen.getByRole('navigation'); |
| 86 | + expect(navigation).toBeInTheDocument(); |
| 87 | + |
| 88 | + const fileButton = within(navigation).getByRole('button', { |
| 89 | + name: /^file$/i |
| 90 | + }); |
| 91 | + expect(fileButton).toBeInTheDocument(); |
| 92 | + |
| 93 | + const newFileButton = within(navigation).getByRole('button', { |
| 94 | + name: /^new$/i |
| 95 | + }); |
| 96 | + expect(newFileButton).toBeInTheDocument(); |
| 97 | + |
| 98 | + // save file button not shown? |
| 99 | + |
| 100 | + // const exampleFileButton = within(navigation).getByRole('link', {name: /^examples$/i}); |
| 101 | + // expect(exampleFileButton).toBeInTheDocument(); |
| 102 | + |
| 103 | + const editButton = within(navigation).getByRole('button', { |
| 104 | + name: /^edit$/i |
| 105 | + }); |
| 106 | + expect(editButton).toBeInTheDocument(); |
| 107 | + |
| 108 | + const sketchButton = within(navigation).getByRole('button', { |
| 109 | + name: /^sketch$/i |
| 110 | + }); |
| 111 | + expect(sketchButton).toBeInTheDocument(); |
| 112 | + |
| 113 | + const helpButton = within(navigation).getByRole('button', { |
| 114 | + name: /^help$/i |
| 115 | + }); |
| 116 | + expect(helpButton).toBeInTheDocument(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('toolbar elements exist', () => { |
| 120 | + const playButton = screen.getByRole('button', { |
| 121 | + name: /play only visual sketch/i |
| 122 | + }); |
| 123 | + expect(playButton).toBeInTheDocument(); |
| 124 | + |
| 125 | + const stopButton = screen.getByRole('button', { |
| 126 | + name: /stop sketch/i |
| 127 | + }); |
| 128 | + expect(stopButton).toBeInTheDocument(); |
| 129 | + |
| 130 | + const editSketchNameButton = screen.getByRole('button', { |
| 131 | + name: /edit sketch name/i |
| 132 | + }); |
| 133 | + expect(editSketchNameButton).toBeInTheDocument(); |
| 134 | + |
| 135 | + expect(screen.getByText('Auto-refresh')).toBeInTheDocument(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('preview exists', () => { |
| 139 | + expect( |
| 140 | + screen.getByRole('heading', { name: /preview/i }) |
| 141 | + ).toBeInTheDocument(); |
| 142 | + const preview = screen.getByRole('main', { name: /sketch output/i }); |
| 143 | + expect(preview).toBeInTheDocument(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('code editor exists', () => { |
| 147 | + const codeeditor = screen.getByRole('article'); |
| 148 | + expect(codeeditor).toBeInTheDocument(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('sidebar exists', () => { |
| 152 | + expect(screen.getByText('Sketch Files')).toBeInTheDocument(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('clicking on play updates the preview iframe with a srcdoc, stop clears it', () => { |
| 156 | + const playButton = screen.getByRole('button', { |
| 157 | + name: /play only visual sketch/i |
| 158 | + }); |
| 159 | + const preview = screen.getByRole('main', { name: /sketch output/i }); |
| 160 | + expect(preview.getAttribute('srcdoc')).toBeFalsy(); |
| 161 | + act(() => { |
| 162 | + fireEvent.click(playButton); |
| 163 | + }); |
| 164 | + |
| 165 | + expect(preview.getAttribute('srcdoc')).toBeTruthy(); |
| 166 | + |
| 167 | + const stopButton = screen.getByRole('button', { |
| 168 | + name: /stop sketch/i |
| 169 | + }); |
| 170 | + act(() => { |
| 171 | + fireEvent.click(stopButton); |
| 172 | + }); |
| 173 | + expect(preview.getAttribute('srcdoc')).toMatch(/(^|")\s*($|")/); |
| 174 | + }); |
| 175 | +}); |
0 commit comments