|
| 1 | +/* eslint-env jest */ |
| 2 | + |
| 3 | +import fetchInject from '../src'; |
| 4 | + |
| 5 | +describe('fetchInject', () => { |
| 6 | + const scriptName = 'test.script.js'; |
| 7 | + const cssName = 'test.css'; |
| 8 | + const scriptText = 'function theBestTeam() { console.log("Dodgers."); }'; |
| 9 | + const cssText = '.baseball{ color: blue; }'; |
| 10 | + const networkError = 'Network response was not ok.'; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + fetch.resetMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + test('should fetch a script in non cors mode', async (done) => { |
| 17 | + const DOMContentLoadedEvent = document.createEvent('Event'); |
| 18 | + DOMContentLoadedEvent.initEvent('DOMContentLoaded', true, true); |
| 19 | + const loadEvent = document.createEvent('Event'); |
| 20 | + loadEvent.initEvent('load', true, true); |
| 21 | + const contentloadedListener = jest.fn(); |
| 22 | + document.addEventListener('DOMContentLoaded', contentloadedListener); |
| 23 | + |
| 24 | + setTimeout(() => { |
| 25 | + const injectedScriptElement = document.getElementsByTagName('script')[0]; |
| 26 | + injectedScriptElement.dispatchEvent(loadEvent); |
| 27 | + }, 10); |
| 28 | + |
| 29 | + fetchInject([{ |
| 30 | + url: `//${scriptName}`, |
| 31 | + options: { method: 'GET', mode: 'no-cors' }, |
| 32 | + }]).then(() => { |
| 33 | + expect(document.getElementsByTagName('script')[0].src) |
| 34 | + .toEqual(`http://${scriptName}/`); |
| 35 | + expect(contentloadedListener).toHaveBeenCalledWith(DOMContentLoadedEvent); |
| 36 | + done(); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + test('should reject the promise of a script fails to load in non cors mode', async (done) => { |
| 41 | + const loadErrorEvent = document.createEvent('Event'); |
| 42 | + loadErrorEvent.initEvent('error', true, true); |
| 43 | + const contentloadedListener = jest.fn(); |
| 44 | + document.addEventListener('DOMContentLoaded', contentloadedListener); |
| 45 | + |
| 46 | + setTimeout(() => { |
| 47 | + const injectedScriptElement = document.getElementsByTagName('script')[0]; |
| 48 | + injectedScriptElement.dispatchEvent(loadErrorEvent); |
| 49 | + }, 10); |
| 50 | + |
| 51 | + fetchInject([{ |
| 52 | + url: `//${scriptName}`, |
| 53 | + options: { method: 'GET', mode: 'no-cors' }, |
| 54 | + }]).catch((err) => { |
| 55 | + expect(document.getElementsByTagName('script')[0].src) |
| 56 | + .toEqual(`http://${scriptName}/`); |
| 57 | + expect(contentloadedListener).not.toHaveBeenCalled(); |
| 58 | + expect(err.message).toEqual(networkError); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should fetch a script in cors mode', async (done) => { |
| 64 | + fetch.mockResponseOnce(scriptText); |
| 65 | + |
| 66 | + fetchInject([{ |
| 67 | + url: `//${scriptName}`, |
| 68 | + options: { method: 'GET', mode: 'cors' }, |
| 69 | + }]).then((res) => { |
| 70 | + expect(res[0].text).toEqual(scriptText); |
| 71 | + expect(fetch.mock.calls.length).toEqual(1); |
| 72 | + done(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + test('should fetch without any options', async (done) => { |
| 77 | + fetch.mockResponseOnce(scriptText); |
| 78 | + |
| 79 | + fetchInject([{ |
| 80 | + url: `//${scriptName}`, |
| 81 | + }]).then((res) => { |
| 82 | + expect(res[0].text).toEqual(scriptText); |
| 83 | + expect(fetch.mock.calls.length).toEqual(1); |
| 84 | + done(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should fetch when only given a url', async (done) => { |
| 89 | + fetch.mockResponseOnce(scriptText); |
| 90 | + |
| 91 | + fetchInject([`//${scriptName}`]).then((res) => { |
| 92 | + expect(res[0].text).toEqual(scriptText); |
| 93 | + expect(fetch.mock.calls.length).toEqual(1); |
| 94 | + done(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + test('should fetch after passed promise resolves', async (done) => { |
| 99 | + const timeBeforeResolve = Date.now(); |
| 100 | + const timeoutTime = 500; |
| 101 | + fetch.mockResponseOnce(scriptText); |
| 102 | + const myPromise = new Promise((resolve) => { |
| 103 | + setTimeout(resolve, timeoutTime); |
| 104 | + }); |
| 105 | + |
| 106 | + fetchInject([`//${scriptName}`], myPromise).then((res) => { |
| 107 | + const timeAfterResolve = Date.now(); |
| 108 | + expect(timeAfterResolve - timeBeforeResolve).toBeGreaterThanOrEqual(timeoutTime); |
| 109 | + expect(res[0].text).toEqual(scriptText); |
| 110 | + expect(fetch.mock.calls.length).toEqual(1); |
| 111 | + done(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + test('should fetch css in cors mode', async (done) => { |
| 116 | + fetch.mockResponseOnce(new Blob([cssText], { type: 'text/css' })); |
| 117 | + |
| 118 | + fetchInject([{ |
| 119 | + url: `//${cssName}`, |
| 120 | + options: { method: 'GET', mode: 'cors' }, |
| 121 | + }]).then((res) => { |
| 122 | + expect(res[0].text).toEqual(cssText); |
| 123 | + expect(fetch.mock.calls.length).toEqual(1); |
| 124 | + done(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + test('should return an error for a failed fetch in cors mode', async (done) => { |
| 129 | + fetch.mockResponses([ |
| 130 | + scriptText, |
| 131 | + { status: 500 }, |
| 132 | + ]); |
| 133 | + |
| 134 | + fetchInject([{ |
| 135 | + url: `//${scriptName}`, |
| 136 | + options: { method: 'GET', mode: 'cors' }, |
| 137 | + }]).catch((err) => { |
| 138 | + expect(err.message).toEqual(networkError); |
| 139 | + done(); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + test('should reject promise, if no scripts are defined', () => { |
| 144 | + expect(fetchInject()).rejects.toThrow("Failed to execute 'fetchInject': 1 argument required but only 0 present."); |
| 145 | + }); |
| 146 | + |
| 147 | + test('should reject promise, if scripts is not an array', () => { |
| 148 | + const errorMessage = "Failed to execute 'fetchInject': argument 1 must be of type 'Array'."; |
| 149 | + |
| 150 | + expect(fetchInject({ an: 'object' })).rejects.toThrow(errorMessage); |
| 151 | + expect(fetchInject('string')).rejects.toThrow(errorMessage); |
| 152 | + expect(fetchInject(true)).rejects.toThrow(errorMessage); |
| 153 | + expect(fetchInject(42)).rejects.toThrow(errorMessage); |
| 154 | + }); |
| 155 | + |
| 156 | + test('should reject promise, the second argument is not a Promise', () => { |
| 157 | + const errorMessage = "Failed to execute 'fetchInject': argument 2 must be of type 'Promise'."; |
| 158 | + |
| 159 | + expect(fetchInject([], { an: 'object' })).rejects.toThrow(errorMessage); |
| 160 | + expect(fetchInject([], 'string')).rejects.toThrow(errorMessage); |
| 161 | + expect(fetchInject([], true)).rejects.toThrow(errorMessage); |
| 162 | + expect(fetchInject([], 42)).rejects.toThrow(errorMessage); |
| 163 | + expect(fetchInject([], [])).rejects.toThrow(errorMessage); |
| 164 | + }); |
| 165 | +}); |
0 commit comments