|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* eslint-disable |
| 4 | + global-require, |
| 5 | + no-undefined |
| 6 | +*/ |
| 7 | +/* global self */ |
| 8 | + |
| 9 | +describe('index', () => { |
| 10 | + let log; |
| 11 | + let socket; |
| 12 | + let overlay; |
| 13 | + let reloadApp; |
| 14 | + let sendMessage; |
| 15 | + let onSocketMessage; |
| 16 | + const locationValue = self.location; |
| 17 | + const resourceQueryValue = global.__resourceQuery; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + global.__resourceQuery = 'foo'; |
| 21 | + self.location.reload = jest.fn(); |
| 22 | + |
| 23 | + // log |
| 24 | + jest.setMock('../../../client-src/default/utils/log.js', { |
| 25 | + log: { |
| 26 | + info: jest.fn(), |
| 27 | + warn: jest.fn(), |
| 28 | + error: jest.fn(), |
| 29 | + }, |
| 30 | + }); |
| 31 | + log = require('../../../client-src/default/utils/log'); |
| 32 | + |
| 33 | + // socket |
| 34 | + jest.setMock('../../../client-src/default/socket.js', jest.fn()); |
| 35 | + socket = require('../../../client-src/default/socket'); |
| 36 | + |
| 37 | + // overlay |
| 38 | + jest.setMock('../../../client-src/default/overlay.js', { |
| 39 | + clear: jest.fn(), |
| 40 | + showMessage: jest.fn(), |
| 41 | + }); |
| 42 | + overlay = require('../../../client-src/default/overlay'); |
| 43 | + |
| 44 | + // reloadApp |
| 45 | + jest.setMock('../../../client-src/default/utils/reloadApp.js', jest.fn()); |
| 46 | + reloadApp = require('../../../client-src/default/utils/reloadApp'); |
| 47 | + |
| 48 | + // sendMessage |
| 49 | + jest.setMock('../../../client-src/default/utils/sendMessage.js', jest.fn()); |
| 50 | + sendMessage = require('../../../client-src/default/utils/sendMessage'); |
| 51 | + |
| 52 | + // createSocketUrl |
| 53 | + jest.setMock( |
| 54 | + '../../../client-src/default/utils/createSocketUrl.js', |
| 55 | + () => 'mock-url' |
| 56 | + ); |
| 57 | + |
| 58 | + require('../../../client-src/default'); |
| 59 | + onSocketMessage = socket.mock.calls[0][1]; |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + global.__resourceQuery = resourceQueryValue; |
| 64 | + Object.assign(self, locationValue); |
| 65 | + jest.resetAllMocks(); |
| 66 | + jest.resetModules(); |
| 67 | + }); |
| 68 | + |
| 69 | + test('should set arguments into socket function', () => { |
| 70 | + expect(socket.mock.calls[0]).toMatchSnapshot(); |
| 71 | + }); |
| 72 | + |
| 73 | + test('should run onSocketMessage.hot', () => { |
| 74 | + onSocketMessage.hot(); |
| 75 | + expect(log.log.info.mock.calls[0][0]).toMatchSnapshot(); |
| 76 | + }); |
| 77 | + |
| 78 | + test('should run onSocketMessage.liveReload', () => { |
| 79 | + onSocketMessage.liveReload(); |
| 80 | + expect(log.log.info.mock.calls[0][0]).toMatchSnapshot(); |
| 81 | + }); |
| 82 | + |
| 83 | + test('should run onSocketMessage.invalid', () => { |
| 84 | + onSocketMessage.invalid(); |
| 85 | + expect(log.log.info.mock.calls[0][0]).toMatchSnapshot(); |
| 86 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 87 | + expect(overlay.clear).not.toBeCalled(); |
| 88 | + |
| 89 | + // change flags |
| 90 | + onSocketMessage.overlay(true); |
| 91 | + onSocketMessage.invalid(); |
| 92 | + expect(overlay.clear).toBeCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + test("should run onSocketMessage['still-ok']", () => { |
| 96 | + onSocketMessage['still-ok'](); |
| 97 | + expect(log.log.info.mock.calls[0][0]).toMatchSnapshot(); |
| 98 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 99 | + expect(overlay.clear).not.toBeCalled(); |
| 100 | + |
| 101 | + // change flags |
| 102 | + onSocketMessage.overlay(true); |
| 103 | + onSocketMessage['still-ok'](); |
| 104 | + expect(overlay.clear).toBeCalled(); |
| 105 | + }); |
| 106 | + |
| 107 | + // TODO: need to mock require.context |
| 108 | + test.skip("should run onSocketMessage['log-level']", () => { |
| 109 | + onSocketMessage['log-level'](); |
| 110 | + }); |
| 111 | + |
| 112 | + test("should run onSocketMessage.progress and onSocketMessage['progress-update']", () => { |
| 113 | + onSocketMessage.progress(false); |
| 114 | + onSocketMessage['progress-update']({ |
| 115 | + msg: 'mock-msg', |
| 116 | + percent: '12', |
| 117 | + }); |
| 118 | + expect(log.log.info).not.toBeCalled(); |
| 119 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 120 | + |
| 121 | + onSocketMessage.progress(true); |
| 122 | + onSocketMessage['progress-update']({ |
| 123 | + msg: 'mock-msg', |
| 124 | + percent: '12', |
| 125 | + }); |
| 126 | + expect(log.log.info.mock.calls[0][0]).toMatchSnapshot(); |
| 127 | + }); |
| 128 | + |
| 129 | + test('should run onSocketMessage.overlay with an argument is Object', () => { |
| 130 | + onSocketMessage.overlay({ |
| 131 | + warnings: true, |
| 132 | + errors: true, |
| 133 | + }); |
| 134 | + |
| 135 | + // check if flags have changed |
| 136 | + onSocketMessage.invalid(); |
| 137 | + expect(overlay.clear).toBeCalled(); |
| 138 | + }); |
| 139 | + |
| 140 | + test('should run onSocketMessage.ok', () => { |
| 141 | + { |
| 142 | + const res = onSocketMessage.ok(); |
| 143 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 144 | + expect(res).toEqual(false); |
| 145 | + } |
| 146 | + |
| 147 | + // change flags |
| 148 | + { |
| 149 | + onSocketMessage.errors([]); |
| 150 | + onSocketMessage.hash('mock-hash'); |
| 151 | + |
| 152 | + const res = onSocketMessage.ok(); |
| 153 | + expect(reloadApp).toBeCalled(); |
| 154 | + expect(reloadApp.mock.calls[0][0]).toMatchSnapshot(); |
| 155 | + expect(res).toEqual(undefined); |
| 156 | + } |
| 157 | + }); |
| 158 | + |
| 159 | + test("should run onSocketMessage['content-changed']", () => { |
| 160 | + onSocketMessage['content-changed'](); |
| 161 | + |
| 162 | + expect(log.log.info.mock.calls[0][0]).toMatchSnapshot(); |
| 163 | + expect(self.location.reload).toBeCalled(); |
| 164 | + }); |
| 165 | + |
| 166 | + test('should run onSocketMessage.warnings', () => { |
| 167 | + { |
| 168 | + const res = onSocketMessage.warnings([ |
| 169 | + 'warn1', |
| 170 | + '\u001B[4mwarn2\u001B[0m', |
| 171 | + 'warn3', |
| 172 | + ]); |
| 173 | + expect(res).toEqual(false); |
| 174 | + |
| 175 | + expect(log.log.warn.mock.calls[0][0]).toMatchSnapshot(); |
| 176 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 177 | + expect(log.log.warn.mock.calls.splice(1)).toMatchSnapshot(); |
| 178 | + } |
| 179 | + |
| 180 | + // change flags |
| 181 | + { |
| 182 | + onSocketMessage.overlay({ |
| 183 | + warnings: true, |
| 184 | + }); |
| 185 | + const res = onSocketMessage.warnings([]); |
| 186 | + expect(res).toEqual(undefined); |
| 187 | + |
| 188 | + expect(overlay.showMessage).toBeCalled(); |
| 189 | + expect(reloadApp).toBeCalled(); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + test('should run onSocketMessage.errors', () => { |
| 194 | + onSocketMessage.errors(['error1', '\u001B[4error2\u001B[0m', 'error3']); |
| 195 | + |
| 196 | + expect(log.log.error.mock.calls[0][0]).toMatchSnapshot(); |
| 197 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 198 | + expect(log.log.error.mock.calls.splice(1)).toMatchSnapshot(); |
| 199 | + |
| 200 | + // change flags |
| 201 | + onSocketMessage.overlay({ |
| 202 | + errors: true, |
| 203 | + }); |
| 204 | + onSocketMessage.errors([]); |
| 205 | + expect(overlay.showMessage).toBeCalled(); |
| 206 | + }); |
| 207 | + |
| 208 | + test('should run onSocketMessage.error', () => { |
| 209 | + onSocketMessage.error('error!!'); |
| 210 | + expect(log.log.error.mock.calls[0][0]).toMatchSnapshot(); |
| 211 | + }); |
| 212 | + |
| 213 | + test('should run onSocketMessage.close', () => { |
| 214 | + onSocketMessage.close(); |
| 215 | + expect(log.log.error.mock.calls[0][0]).toMatchSnapshot(); |
| 216 | + expect(sendMessage.mock.calls[0][0]).toMatchSnapshot(); |
| 217 | + }); |
| 218 | +}); |
0 commit comments