|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { expect } from 'chai'; |
| 19 | +import * as mockFetch from './mock_fetch'; |
| 20 | + |
| 21 | +async function fetchJson(path: string, req?: object): Promise<object> { |
| 22 | + const body = req |
| 23 | + ? { |
| 24 | + body: JSON.stringify(req) |
| 25 | + } |
| 26 | + : {}; |
| 27 | + const request: RequestInit = { |
| 28 | + method: 'POST', |
| 29 | + headers: { |
| 30 | + 'Content-Type': 'application/json' |
| 31 | + }, |
| 32 | + referrerPolicy: 'no-referrer', |
| 33 | + ...body |
| 34 | + }; |
| 35 | + |
| 36 | + const response = await fetch(path, request); |
| 37 | + return response.json(); |
| 38 | +} |
| 39 | + |
| 40 | +describe('mock fetch utility', () => { |
| 41 | + beforeEach(mockFetch.setUp); |
| 42 | + afterEach(mockFetch.tearDown); |
| 43 | + |
| 44 | + describe('matched requests', () => { |
| 45 | + it('returns the correct object for multiple routes', async () => { |
| 46 | + mockFetch.mock('/a', { a: 1 }); |
| 47 | + mockFetch.mock('/b', { b: 2 }); |
| 48 | + |
| 49 | + expect(await fetchJson('/a')).to.eql({ a: 1 }); |
| 50 | + expect(await fetchJson('/b')).to.eql({ b: 2 }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('passes through the status of the mock', async () => { |
| 54 | + mockFetch.mock('/not-ok', {}, 500); |
| 55 | + expect((await fetch('/not-ok')).status).to.equal(500); |
| 56 | + }); |
| 57 | + |
| 58 | + it('records calls to the mock', async () => { |
| 59 | + const someRequest = { |
| 60 | + sentence: |
| 61 | + 'Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo' |
| 62 | + }; |
| 63 | + |
| 64 | + const mock = mockFetch.mock('/word', {}); |
| 65 | + await fetchJson('/word', someRequest); |
| 66 | + await fetchJson('/word', { a: 'b' }); |
| 67 | + await fetch('/word'); |
| 68 | + |
| 69 | + expect(mock.calls.length).to.equal(3); |
| 70 | + expect(mock.calls[0].request).to.eql(someRequest); |
| 71 | + expect(mock.calls[1].request).to.eql({ a: 'b' }); |
| 72 | + expect(mock.calls[2].request).to.equal(undefined); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('route rejection', () => { |
| 77 | + it('if the route is not in the map', () => { |
| 78 | + mockFetch.mock('/test', {}); |
| 79 | + expect(() => fetch('/not-test')).to.throw( |
| 80 | + 'Unknown route being requested: /not-test' |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it('if call is not a string', () => { |
| 85 | + mockFetch.mock('/blah', {}); |
| 86 | + expect(() => fetch(new Request({} as any))).to.throw( |
| 87 | + 'URL passed to fetch was not a string' |
| 88 | + ); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +describe('mock fetch utility (no setUp/tearDown)', () => { |
| 94 | + it('errors if mock attempted without setup', () => { |
| 95 | + expect(() => mockFetch.mock('/test', {})).to.throw( |
| 96 | + 'Mock fetch is not set up' |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('routes do not carry to next run', async () => { |
| 101 | + mockFetch.setUp(); |
| 102 | + mockFetch.mock('/test', { first: 'first' }); |
| 103 | + expect(await fetchJson('/test')).to.eql({ first: 'first' }); |
| 104 | + mockFetch.tearDown(); |
| 105 | + mockFetch.setUp(); |
| 106 | + expect(() => fetch('/test')).to.throw( |
| 107 | + 'Unknown route being requested: /test' |
| 108 | + ); |
| 109 | + mockFetch.tearDown(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments