|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import nock from 'nock'; |
| 3 | +import { defineComponent, h, nextTick } from 'vue'; |
| 4 | + |
| 5 | +// Reuse the same endpoints/fixtures used by the React tests |
| 6 | +import { |
| 7 | + CoolerArticleResource, |
| 8 | + StaticArticleResource, |
| 9 | +} from '../../../../__tests__/new'; |
| 10 | +// Minimal shared fixture (copied from React test fixtures) |
| 11 | +const payload = { |
| 12 | + id: 5, |
| 13 | + title: 'hi ho', |
| 14 | + content: 'whatever', |
| 15 | + tags: ['a', 'best', 'react'], |
| 16 | +}; |
| 17 | +import useFetch from '../consumers/useFetch'; |
| 18 | +import { provideDataClient } from '../providers/provideDataClient'; |
| 19 | + |
| 20 | +async function flush() { |
| 21 | + await Promise.resolve(); |
| 22 | + await nextTick(); |
| 23 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 24 | +} |
| 25 | + |
| 26 | +async function flushUntil(wrapper: any, predicate: () => boolean, tries = 100) { |
| 27 | + for (let i = 0; i < tries; i++) { |
| 28 | + if (predicate()) return; |
| 29 | + await flush(); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe('vue useFetch()', () => { |
| 34 | + let mynock: nock.Scope; |
| 35 | + beforeAll(() => { |
| 36 | + nock(/.*/) |
| 37 | + .persist() |
| 38 | + .defaultReplyHeaders({ |
| 39 | + 'Access-Control-Allow-Origin': '*', |
| 40 | + 'Access-Control-Allow-Headers': 'Access-Token', |
| 41 | + 'Content-Type': 'application/json', |
| 42 | + }) |
| 43 | + .options(/.*/) |
| 44 | + .reply(200); |
| 45 | + }); |
| 46 | + beforeEach(() => { |
| 47 | + mynock = nock(/.*/).defaultReplyHeaders({ |
| 48 | + 'Access-Control-Allow-Origin': '*', |
| 49 | + 'Access-Control-Allow-Headers': 'Access-Token', |
| 50 | + 'Content-Type': 'application/json', |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + nock.cleanAll(); |
| 56 | + }); |
| 57 | + |
| 58 | + const ProvideWrapper = defineComponent({ |
| 59 | + name: 'ProvideWrapper', |
| 60 | + setup(_props, { slots, expose }) { |
| 61 | + const { controller } = provideDataClient(); |
| 62 | + expose({ controller }); |
| 63 | + return () => (slots.default ? slots.default() : h('div')); |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + it('should dispatch singles', async () => { |
| 68 | + const fetchMock = jest.fn(() => payload); |
| 69 | + mynock.get(`/article-cooler/${payload.id}`).reply(200, fetchMock); |
| 70 | + |
| 71 | + const Comp = defineComponent({ |
| 72 | + name: 'FetchTester', |
| 73 | + setup() { |
| 74 | + const p = useFetch(CoolerArticleResource.get, { id: payload.id }); |
| 75 | + return () => h('div', { class: 'root' }, String(!!p)); |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const wrapper = mount(ProvideWrapper, { |
| 80 | + slots: { default: () => h(Comp) }, |
| 81 | + }); |
| 82 | + |
| 83 | + // Wait for the fetch to happen |
| 84 | + await flushUntil(wrapper, () => fetchMock.mock.calls.length > 0); |
| 85 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should not dispatch with null params, then dispatch after set', async () => { |
| 89 | + const fetchMock = jest.fn(() => payload); |
| 90 | + mynock.get(`/article-cooler/${payload.id}`).reply(200, fetchMock); |
| 91 | + |
| 92 | + let params: any = null; |
| 93 | + const Comp = defineComponent({ |
| 94 | + name: 'FetchTesterNull', |
| 95 | + setup() { |
| 96 | + // reactive params via closure re-render with slot remount |
| 97 | + useFetch(CoolerArticleResource.get as any, params); |
| 98 | + return () => h('div'); |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + const wrapper = mount(ProvideWrapper, { |
| 103 | + slots: { default: () => h(Comp) }, |
| 104 | + }); |
| 105 | + await flush(); |
| 106 | + expect(fetchMock).toHaveBeenCalledTimes(0); |
| 107 | + |
| 108 | + // change params and remount child to re-run setup |
| 109 | + params = { id: payload.id }; |
| 110 | + wrapper.unmount(); |
| 111 | + const wrapper2 = mount(ProvideWrapper, { |
| 112 | + slots: { default: () => h(Comp) }, |
| 113 | + }); |
| 114 | + await flushUntil(wrapper2, () => fetchMock.mock.calls.length > 0); |
| 115 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should respect expiry and not refetch when fresh', async () => { |
| 119 | + const fetchMock = jest.fn(() => payload); |
| 120 | + mynock.get(`/article-cooler/${payload.id}`).reply(200, fetchMock); |
| 121 | + |
| 122 | + const Child = defineComponent({ |
| 123 | + name: 'FetchChild', |
| 124 | + setup() { |
| 125 | + useFetch(CoolerArticleResource.get, { id: payload.id }); |
| 126 | + return () => h('div'); |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + const Parent = defineComponent({ |
| 131 | + name: 'Parent', |
| 132 | + setup(_props, { expose }) { |
| 133 | + const { controller } = provideDataClient(); |
| 134 | + let idx = 0; |
| 135 | + const remount = () => { |
| 136 | + idx++; |
| 137 | + }; |
| 138 | + expose({ controller, remount }); |
| 139 | + return () => h('div', [h(Child, { key: idx })]); |
| 140 | + }, |
| 141 | + }); |
| 142 | + |
| 143 | + const wrapper = mount(Parent); |
| 144 | + await flushUntil(wrapper, () => fetchMock.mock.calls.length > 0); |
| 145 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 146 | + |
| 147 | + // Remount child inside same provider should not refetch while data is fresh |
| 148 | + (wrapper.vm as any).remount(); |
| 149 | + await flush(); |
| 150 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should dispatch with resource and endpoint expiry overrides', async () => { |
| 154 | + const mock1 = jest.fn(() => payload); |
| 155 | + const mock2 = jest.fn(() => payload); |
| 156 | + const mock3 = jest.fn(() => payload); |
| 157 | + |
| 158 | + mynock |
| 159 | + .get(`/article-static/${payload.id}`) |
| 160 | + .reply(200, mock1) |
| 161 | + .get(`/article-static/${payload.id}`) |
| 162 | + .reply(200, mock2) |
| 163 | + .get(`/article-static/${payload.id}`) |
| 164 | + .reply(200, mock3); |
| 165 | + |
| 166 | + const Comp1 = defineComponent({ |
| 167 | + name: 'FetchStaticGet', |
| 168 | + setup() { |
| 169 | + useFetch(StaticArticleResource.get, { id: payload.id }); |
| 170 | + return () => h('div'); |
| 171 | + }, |
| 172 | + }); |
| 173 | + const Comp2 = defineComponent({ |
| 174 | + name: 'FetchStaticLong', |
| 175 | + setup() { |
| 176 | + useFetch(StaticArticleResource.longLiving, { id: payload.id }); |
| 177 | + return () => h('div'); |
| 178 | + }, |
| 179 | + }); |
| 180 | + const Comp3 = defineComponent({ |
| 181 | + name: 'FetchStaticNeverRetry', |
| 182 | + setup() { |
| 183 | + useFetch(StaticArticleResource.neverRetryOnError, { id: payload.id }); |
| 184 | + return () => h('div'); |
| 185 | + }, |
| 186 | + }); |
| 187 | + |
| 188 | + const w1 = mount(ProvideWrapper, { slots: { default: () => h(Comp1) } }); |
| 189 | + await flushUntil(w1, () => mock1.mock.calls.length > 0); |
| 190 | + expect(mock1).toHaveBeenCalled(); |
| 191 | + |
| 192 | + const w2 = mount(ProvideWrapper, { slots: { default: () => h(Comp2) } }); |
| 193 | + await flushUntil(w2, () => mock2.mock.calls.length > 0); |
| 194 | + expect(mock2).toHaveBeenCalled(); |
| 195 | + |
| 196 | + const w3 = mount(ProvideWrapper, { slots: { default: () => h(Comp3) } }); |
| 197 | + await flushUntil(w3, () => mock3.mock.calls.length > 0); |
| 198 | + expect(mock3).toHaveBeenCalled(); |
| 199 | + }); |
| 200 | +}); |
0 commit comments