|
| 1 | +import { allSettled, createStore, fork } from 'effector'; |
| 2 | +import { describe, test, expect, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { createApiRequest, type FetchOptions } from '../api'; |
| 5 | +import { fetchFx } from '../fetch'; |
| 6 | + |
| 7 | +describe('fetch/api.request.fetchOptions', () => { |
| 8 | + // Does not matter |
| 9 | + const mapBody = () => 'any body'; |
| 10 | + const url = 'https://api.salo.com'; |
| 11 | + const method = 'GET'; |
| 12 | + |
| 13 | + // Does not matter |
| 14 | + const response = { |
| 15 | + extract: async <T>(v: T) => v, |
| 16 | + }; |
| 17 | + |
| 18 | + test('pass static fetchOptions on creation to request', async () => { |
| 19 | + const callApiFx = createApiRequest({ |
| 20 | + request: { |
| 21 | + mapBody, |
| 22 | + method, |
| 23 | + url, |
| 24 | + fetchOptions: { |
| 25 | + mode: 'cors', |
| 26 | + cache: 'no-cache', |
| 27 | + referrerPolicy: 'no-referrer', |
| 28 | + }, |
| 29 | + }, |
| 30 | + response, |
| 31 | + }); |
| 32 | + |
| 33 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 34 | + |
| 35 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 36 | + |
| 37 | + await allSettled(callApiFx, { scope, params: {} }); |
| 38 | + |
| 39 | + const request = fetchMock.mock.calls[0][0] as Request; |
| 40 | + expect(request.mode).toEqual('cors'); |
| 41 | + expect(request.cache).toEqual('no-cache'); |
| 42 | + expect(request.referrerPolicy).toEqual('no-referrer'); |
| 43 | + }); |
| 44 | + |
| 45 | + test('pass reactive fetchOptions on creation to request', async () => { |
| 46 | + const $fetchOptions = createStore<FetchOptions>({ |
| 47 | + mode: 'cors', |
| 48 | + cache: 'no-cache', |
| 49 | + }); |
| 50 | + |
| 51 | + const callApiFx = createApiRequest({ |
| 52 | + request: { mapBody, method, url, fetchOptions: $fetchOptions }, |
| 53 | + response, |
| 54 | + }); |
| 55 | + |
| 56 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 57 | + |
| 58 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 59 | + |
| 60 | + // with original value |
| 61 | + await allSettled(callApiFx, { scope, params: {} }); |
| 62 | + let request = fetchMock.mock.calls[0][0] as Request; |
| 63 | + expect(request.mode).toEqual('cors'); |
| 64 | + expect(request.cache).toEqual('no-cache'); |
| 65 | + |
| 66 | + // with new value |
| 67 | + await allSettled($fetchOptions, { |
| 68 | + scope, |
| 69 | + params: { mode: 'no-cors', cache: 'force-cache' }, |
| 70 | + }); |
| 71 | + await allSettled(callApiFx, { scope, params: {} }); |
| 72 | + request = fetchMock.mock.calls[1][0] as Request; |
| 73 | + expect(request.mode).toEqual('no-cors'); |
| 74 | + expect(request.cache).toEqual('force-cache'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('top-level credentials takes precedence over fetchOptions.credentials', async () => { |
| 78 | + const callApiFx = createApiRequest({ |
| 79 | + request: { |
| 80 | + mapBody, |
| 81 | + method, |
| 82 | + url, |
| 83 | + credentials: 'include', |
| 84 | + fetchOptions: { |
| 85 | + credentials: 'omit', |
| 86 | + cache: 'no-cache', |
| 87 | + }, |
| 88 | + }, |
| 89 | + response, |
| 90 | + }); |
| 91 | + |
| 92 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 93 | + |
| 94 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 95 | + |
| 96 | + await allSettled(callApiFx, { scope, params: {} }); |
| 97 | + |
| 98 | + const request = fetchMock.mock.calls[0][0] as Request; |
| 99 | + // top-level credentials should win |
| 100 | + expect(request.credentials).toEqual('include'); |
| 101 | + // other fetchOptions should still apply |
| 102 | + expect(request.cache).toEqual('no-cache'); |
| 103 | + }); |
| 104 | + |
| 105 | + test('fetchOptions.credentials is used when top-level credentials is not set', async () => { |
| 106 | + const callApiFx = createApiRequest({ |
| 107 | + request: { |
| 108 | + mapBody, |
| 109 | + method, |
| 110 | + url, |
| 111 | + fetchOptions: { |
| 112 | + credentials: 'include', |
| 113 | + }, |
| 114 | + }, |
| 115 | + response, |
| 116 | + }); |
| 117 | + |
| 118 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 119 | + |
| 120 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 121 | + |
| 122 | + await allSettled(callApiFx, { scope, params: {} }); |
| 123 | + |
| 124 | + const request = fetchMock.mock.calls[0][0] as Request; |
| 125 | + expect(request.credentials).toEqual('include'); |
| 126 | + }); |
| 127 | + |
| 128 | + test('pass fetchOptions with keepalive option', async () => { |
| 129 | + const callApiFx = createApiRequest({ |
| 130 | + request: { |
| 131 | + mapBody, |
| 132 | + method, |
| 133 | + url, |
| 134 | + fetchOptions: { |
| 135 | + keepalive: true, |
| 136 | + }, |
| 137 | + }, |
| 138 | + response, |
| 139 | + }); |
| 140 | + |
| 141 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 142 | + |
| 143 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 144 | + |
| 145 | + await allSettled(callApiFx, { scope, params: {} }); |
| 146 | + |
| 147 | + const request = fetchMock.mock.calls[0][0] as Request; |
| 148 | + expect(request.keepalive).toEqual(true); |
| 149 | + }); |
| 150 | + |
| 151 | + test('pass fetchOptions with redirect option', async () => { |
| 152 | + const callApiFx = createApiRequest({ |
| 153 | + request: { |
| 154 | + mapBody, |
| 155 | + method, |
| 156 | + url, |
| 157 | + fetchOptions: { |
| 158 | + redirect: 'manual', |
| 159 | + }, |
| 160 | + }, |
| 161 | + response, |
| 162 | + }); |
| 163 | + |
| 164 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 165 | + |
| 166 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 167 | + |
| 168 | + await allSettled(callApiFx, { scope, params: {} }); |
| 169 | + |
| 170 | + const request = fetchMock.mock.calls[0][0] as Request; |
| 171 | + expect(request.redirect).toEqual('manual'); |
| 172 | + }); |
| 173 | + |
| 174 | + test('pass fetchOptions with integrity option', async () => { |
| 175 | + const integrityValue = |
| 176 | + 'sha384-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC'; |
| 177 | + |
| 178 | + const callApiFx = createApiRequest({ |
| 179 | + request: { |
| 180 | + mapBody, |
| 181 | + method, |
| 182 | + url, |
| 183 | + fetchOptions: { |
| 184 | + integrity: integrityValue, |
| 185 | + }, |
| 186 | + }, |
| 187 | + response, |
| 188 | + }); |
| 189 | + |
| 190 | + const fetchMock = vi.fn().mockResolvedValue(new Response('test')); |
| 191 | + |
| 192 | + const scope = fork({ handlers: [[fetchFx, fetchMock]] }); |
| 193 | + |
| 194 | + await allSettled(callApiFx, { scope, params: {} }); |
| 195 | + |
| 196 | + const request = fetchMock.mock.calls[0][0] as Request; |
| 197 | + expect(request.integrity).toEqual(integrityValue); |
| 198 | + }); |
| 199 | +}); |
0 commit comments