|
| 1 | +import { assert } from 'chai'; |
| 2 | +import fetchMock from 'fetch-mock'; |
| 3 | +import { RelayNetworkLayer } from '../../src'; |
| 4 | +import { mockReq } from '../testutils'; |
| 5 | +import urlMiddleware from '../../src/middleware/url'; |
| 6 | + |
| 7 | +describe('Middleware / url', () => { |
| 8 | + describe('url option as string', () => { |
| 9 | + const rnl = new RelayNetworkLayer([ |
| 10 | + urlMiddleware({ |
| 11 | + url: '/other/url', |
| 12 | + }), |
| 13 | + ]); |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + fetchMock.restore(); |
| 17 | + |
| 18 | + fetchMock.mock({ |
| 19 | + matcher: '/other/url', |
| 20 | + response: { |
| 21 | + status: 200, |
| 22 | + body: { data: 'PAYLOAD' }, |
| 23 | + sendAsJson: true, |
| 24 | + }, |
| 25 | + method: 'POST', |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should work with query', () => { |
| 30 | + const req1 = mockReq(); |
| 31 | + rnl.sendQueries([req1]).then(() => { |
| 32 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should work with mutation', () => { |
| 37 | + const req1 = mockReq(); |
| 38 | + return assert.isFulfilled( |
| 39 | + rnl.sendMutation(req1).then(() => { |
| 40 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 41 | + }) |
| 42 | + ); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('url option as thunk', () => { |
| 47 | + const rnl = new RelayNetworkLayer([ |
| 48 | + urlMiddleware({ |
| 49 | + url: (_) => '/thunk_url', // eslint-disable-line |
| 50 | + }), |
| 51 | + ]); |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + fetchMock.restore(); |
| 55 | + |
| 56 | + fetchMock.mock({ |
| 57 | + matcher: '/thunk_url', |
| 58 | + response: { |
| 59 | + status: 200, |
| 60 | + body: { data: 'PAYLOAD' }, |
| 61 | + sendAsJson: true, |
| 62 | + }, |
| 63 | + method: 'POST', |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should work with query', () => { |
| 68 | + const req1 = mockReq(); |
| 69 | + rnl.sendQueries([req1]).then(() => { |
| 70 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should work with mutation', () => { |
| 75 | + const req1 = mockReq(); |
| 76 | + return assert.isFulfilled( |
| 77 | + rnl.sendMutation(req1).then(() => { |
| 78 | + assert.equal(req1.payload.response, 'PAYLOAD'); |
| 79 | + }) |
| 80 | + ); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments