|
17 | 17 | * limitations under the License.
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -import { |
21 |
| - FakeXMLHttpRequest, |
22 |
| - FakeXMLHttpRequestStatic, |
23 |
| - fakeXhr |
24 |
| -} from 'nise' |
25 |
| -import { makeGetRequest } from '../src/browserRequest' |
| 20 | +import { FakeXMLHttpRequest, FakeXMLHttpRequestStatic, fakeXhr } from 'nise'; |
| 21 | +import { makeGetRequest } from '../src/browserRequest'; |
26 | 22 |
|
27 | 23 | describe('browserRequest', () => {
|
28 | 24 | describe('makeGetRequest', () => {
|
29 |
| - let mockXHR: FakeXMLHttpRequestStatic |
30 |
| - let xhrs: FakeXMLHttpRequest[] |
| 25 | + let mockXHR: FakeXMLHttpRequestStatic; |
| 26 | + let xhrs: FakeXMLHttpRequest[]; |
31 | 27 | beforeEach(() => {
|
32 |
| - xhrs = [] |
33 |
| - mockXHR = fakeXhr.useFakeXMLHttpRequest() |
34 |
| - mockXHR.onCreate = req => xhrs.push(req) |
35 |
| - }) |
| 28 | + xhrs = []; |
| 29 | + mockXHR = fakeXhr.useFakeXMLHttpRequest(); |
| 30 | + mockXHR.onCreate = (req): number => xhrs.push(req); |
| 31 | + }); |
36 | 32 |
|
37 | 33 | afterEach(() => {
|
38 |
| - mockXHR.restore() |
39 |
| - }) |
| 34 | + mockXHR.restore(); |
| 35 | + }); |
40 | 36 |
|
41 | 37 | it('makes a GET request to the argument URL', async () => {
|
42 |
| - const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}) |
| 38 | + const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}); |
43 | 39 |
|
44 |
| - expect(xhrs.length).toBe(1) |
45 |
| - const xhr = xhrs[0] |
46 |
| - const { url, method } = xhr |
| 40 | + expect(xhrs.length).toBe(1); |
| 41 | + const xhr = xhrs[0]; |
| 42 | + const { url, method } = xhr; |
47 | 43 | expect({ url, method }).toEqual({
|
48 | 44 | url: 'https://cdn.optimizely.com/datafiles/123.json',
|
49 | 45 | method: 'GET',
|
50 |
| - }) |
| 46 | + }); |
51 | 47 |
|
52 |
| - xhr.respond(200, {}, '{"foo":"bar"}') |
| 48 | + xhr.respond(200, {}, '{"foo":"bar"}'); |
53 | 49 |
|
54 |
| - await req.responsePromise |
55 |
| - }) |
| 50 | + await req.responsePromise; |
| 51 | + }); |
56 | 52 |
|
57 | 53 | it('returns a 200 response back to its superclass', async () => {
|
58 |
| - const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}) |
| 54 | + const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}); |
59 | 55 |
|
60 |
| - const xhr = xhrs[0] |
61 |
| - xhr.respond(200, {}, '{"foo":"bar"}') |
| 56 | + const xhr = xhrs[0]; |
| 57 | + xhr.respond(200, {}, '{"foo":"bar"}'); |
62 | 58 |
|
63 |
| - const resp = await req.responsePromise |
| 59 | + const resp = await req.responsePromise; |
64 | 60 | expect(resp).toEqual({
|
65 | 61 | statusCode: 200,
|
66 | 62 | headers: {},
|
67 | 63 | body: '{"foo":"bar"}',
|
68 |
| - }) |
69 |
| - }) |
| 64 | + }); |
| 65 | + }); |
70 | 66 |
|
71 | 67 | it('returns a 404 response back to its superclass', async () => {
|
72 |
| - const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}) |
| 68 | + const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}); |
73 | 69 |
|
74 |
| - const xhr = xhrs[0] |
75 |
| - xhr.respond(404, {}, '') |
| 70 | + const xhr = xhrs[0]; |
| 71 | + xhr.respond(404, {}, ''); |
76 | 72 |
|
77 |
| - const resp = await req.responsePromise |
| 73 | + const resp = await req.responsePromise; |
78 | 74 | expect(resp).toEqual({
|
79 | 75 | statusCode: 404,
|
80 | 76 | headers: {},
|
81 | 77 | body: '',
|
82 |
| - }) |
83 |
| - }) |
| 78 | + }); |
| 79 | + }); |
84 | 80 |
|
85 | 81 | it('includes headers from the headers argument in the request', async () => {
|
86 | 82 | const req = makeGetRequest('https://cdn.optimizely.com/dataifles/123.json', {
|
87 | 83 | 'if-modified-since': 'Fri, 08 Mar 2019 18:57:18 GMT',
|
88 |
| - }) |
| 84 | + }); |
89 | 85 |
|
90 |
| - expect(xhrs.length).toBe(1) |
91 |
| - expect(xhrs[0].requestHeaders['if-modified-since']).toBe( |
92 |
| - 'Fri, 08 Mar 2019 18:57:18 GMT', |
93 |
| - ) |
| 86 | + expect(xhrs.length).toBe(1); |
| 87 | + expect(xhrs[0].requestHeaders['if-modified-since']).toBe('Fri, 08 Mar 2019 18:57:18 GMT'); |
94 | 88 |
|
95 |
| - xhrs[0].respond(404, {}, '') |
| 89 | + xhrs[0].respond(404, {}, ''); |
96 | 90 |
|
97 |
| - await req.responsePromise |
98 |
| - }) |
| 91 | + await req.responsePromise; |
| 92 | + }); |
99 | 93 |
|
100 | 94 | it('includes headers from the response in the eventual response in the return value', async () => {
|
101 |
| - const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}) |
| 95 | + const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}); |
102 | 96 |
|
103 |
| - const xhr = xhrs[0] |
| 97 | + const xhr = xhrs[0]; |
104 | 98 | xhr.respond(
|
105 | 99 | 200,
|
106 | 100 | {
|
107 | 101 | 'content-type': 'application/json',
|
108 | 102 | 'last-modified': 'Fri, 08 Mar 2019 18:57:18 GMT',
|
109 | 103 | },
|
110 |
| - '{"foo":"bar"}', |
111 |
| - ) |
| 104 | + '{"foo":"bar"}' |
| 105 | + ); |
112 | 106 |
|
113 |
| - const resp = await req.responsePromise |
| 107 | + const resp = await req.responsePromise; |
114 | 108 | expect(resp).toEqual({
|
115 | 109 | statusCode: 200,
|
116 | 110 | body: '{"foo":"bar"}',
|
117 | 111 | headers: {
|
118 | 112 | 'content-type': 'application/json',
|
119 | 113 | 'last-modified': 'Fri, 08 Mar 2019 18:57:18 GMT',
|
120 | 114 | },
|
121 |
| - }) |
122 |
| - }) |
| 115 | + }); |
| 116 | + }); |
123 | 117 |
|
124 | 118 | it('returns a rejected promise when there is a request error', async () => {
|
125 |
| - const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}) |
126 |
| - xhrs[0].error() |
127 |
| - await expect(req.responsePromise).rejects.toThrow() |
128 |
| - }) |
| 119 | + const req = makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}); |
| 120 | + xhrs[0].error(); |
| 121 | + await expect(req.responsePromise).rejects.toThrow(); |
| 122 | + }); |
129 | 123 |
|
130 | 124 | it('sets a timeout on the request object', () => {
|
131 |
| - const onCreateMock = jest.fn() |
132 |
| - mockXHR.onCreate = onCreateMock |
133 |
| - makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}) |
134 |
| - expect(onCreateMock).toBeCalledTimes(1) |
135 |
| - expect(onCreateMock.mock.calls[0][0].timeout).toBe(60000) |
136 |
| - }) |
137 |
| - }) |
138 |
| -}) |
| 125 | + const onCreateMock = jest.fn(); |
| 126 | + mockXHR.onCreate = onCreateMock; |
| 127 | + makeGetRequest('https://cdn.optimizely.com/datafiles/123.json', {}); |
| 128 | + expect(onCreateMock).toBeCalledTimes(1); |
| 129 | + expect(onCreateMock.mock.calls[0][0].timeout).toBe(60000); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments