|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import nock from 'nock'; |
| 3 | +import { defineComponent, h, nextTick, Suspense } from 'vue'; |
| 4 | + |
| 5 | +// Reuse the same endpoints/fixtures used by the React tests |
| 6 | +import { CoolerArticleResource } from '../../../../__tests__/new'; |
| 7 | +// Minimal shared fixture (copied from React test fixtures) |
| 8 | +const payload = { |
| 9 | + id: 5, |
| 10 | + title: 'hi ho', |
| 11 | + content: 'whatever', |
| 12 | + tags: ['a', 'best', 'react'], |
| 13 | +}; |
| 14 | +import useSuspense from '../consumers/useSuspense'; |
| 15 | +import { provideDataClient } from '../providers/provideDataClient'; |
| 16 | + |
| 17 | +describe('vue useSuspense()', () => { |
| 18 | + async function flushUntil( |
| 19 | + wrapper: any, |
| 20 | + predicate: () => boolean, |
| 21 | + tries = 100, |
| 22 | + ) { |
| 23 | + for (let i = 0; i < tries; i++) { |
| 24 | + if (predicate()) return; |
| 25 | + await Promise.resolve(); |
| 26 | + await nextTick(); |
| 27 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + beforeAll(() => { |
| 32 | + nock(/.*/) |
| 33 | + .persist() |
| 34 | + .defaultReplyHeaders({ |
| 35 | + 'Access-Control-Allow-Origin': '*', |
| 36 | + 'Access-Control-Allow-Headers': 'Access-Token', |
| 37 | + 'Content-Type': 'application/json', |
| 38 | + }) |
| 39 | + .options(/.*/) |
| 40 | + .reply(200) |
| 41 | + .get(`/article-cooler/${payload.id}`) |
| 42 | + .reply(200, payload) |
| 43 | + .put(`/article-cooler/${payload.id}`) |
| 44 | + .reply(200, (uri, requestBody: any) => ({ |
| 45 | + ...payload, |
| 46 | + ...requestBody, |
| 47 | + })); |
| 48 | + }); |
| 49 | + |
| 50 | + afterAll(() => { |
| 51 | + nock.cleanAll(); |
| 52 | + }); |
| 53 | + |
| 54 | + const ArticleComp = defineComponent({ |
| 55 | + name: 'ArticleComp', |
| 56 | + async setup() { |
| 57 | + const article = await useSuspense(CoolerArticleResource.get, { |
| 58 | + id: payload.id, |
| 59 | + }); |
| 60 | + return () => |
| 61 | + h('div', [ |
| 62 | + h('h3', (article as any).value.title), |
| 63 | + h('p', (article as any).value.content), |
| 64 | + ]); |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + const ProvideWrapper = defineComponent({ |
| 69 | + name: 'ProvideWrapper', |
| 70 | + setup(_props, { slots, expose }) { |
| 71 | + const { controller } = provideDataClient(); |
| 72 | + expose({ controller }); |
| 73 | + return () => |
| 74 | + h( |
| 75 | + Suspense, |
| 76 | + {}, |
| 77 | + { |
| 78 | + default: () => (slots.default ? slots.default() : h(ArticleComp)), |
| 79 | + fallback: () => h('div', { class: 'fallback' }, 'Loading'), |
| 80 | + }, |
| 81 | + ); |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + it('suspends on empty store, then renders after fetch resolves', async () => { |
| 86 | + const wrapper = mount(ProvideWrapper, { |
| 87 | + slots: { default: () => h(ArticleComp) }, |
| 88 | + }); |
| 89 | + |
| 90 | + // Initially should render fallback while Suspense is pending |
| 91 | + expect(wrapper.find('.fallback').exists()).toBe(true); |
| 92 | + |
| 93 | + // Flush pending promises/ticks until content renders |
| 94 | + await flushUntil(wrapper, () => wrapper.find('h3').exists()); |
| 95 | + |
| 96 | + const title = wrapper.find('h3'); |
| 97 | + const content = wrapper.find('p'); |
| 98 | + expect(title.exists()).toBe(true); |
| 99 | + expect(content.exists()).toBe(true); |
| 100 | + expect(title.text()).toBe(payload.title); |
| 101 | + expect(content.text()).toBe(payload.content); |
| 102 | + }); |
| 103 | + |
| 104 | + it('re-renders when controller.setResponse() updates data', async () => { |
| 105 | + const wrapper = mount(ProvideWrapper, { |
| 106 | + slots: { default: () => h(ArticleComp) }, |
| 107 | + }); |
| 108 | + // Wait for initial render |
| 109 | + await flushUntil(wrapper, () => wrapper.find('h3').exists()); |
| 110 | + |
| 111 | + // Verify initial values |
| 112 | + expect(wrapper.find('h3').text()).toBe(payload.title); |
| 113 | + expect(wrapper.find('p').text()).toBe(payload.content); |
| 114 | + |
| 115 | + // Update the store using controller.setResponse |
| 116 | + const exposed: any = wrapper.vm as any; |
| 117 | + const newTitle = payload.title + ' updated'; |
| 118 | + const newContent = (payload as any).content + ' v2'; |
| 119 | + exposed.controller.setResponse( |
| 120 | + CoolerArticleResource.get, |
| 121 | + { id: payload.id }, |
| 122 | + { ...payload, title: newTitle, content: newContent }, |
| 123 | + ); |
| 124 | + |
| 125 | + await flushUntil(wrapper, () => wrapper.find('h3').text() === newTitle); |
| 126 | + |
| 127 | + expect(wrapper.find('h3').text()).toBe(newTitle); |
| 128 | + expect(wrapper.find('p').text()).toBe(newContent); |
| 129 | + }); |
| 130 | + |
| 131 | + it('re-renders when controller.fetch() mutates data', async () => { |
| 132 | + const wrapper = mount(ProvideWrapper, { |
| 133 | + slots: { default: () => h(ArticleComp) }, |
| 134 | + }); |
| 135 | + // Wait for initial render |
| 136 | + await flushUntil(wrapper, () => wrapper.find('h3').exists()); |
| 137 | + |
| 138 | + // Verify initial values |
| 139 | + expect(wrapper.find('h3').text()).toBe(payload.title); |
| 140 | + expect(wrapper.find('p').text()).toBe(payload.content); |
| 141 | + |
| 142 | + // Mutate the data using controller.fetch with update endpoint |
| 143 | + const exposed: any = wrapper.vm as any; |
| 144 | + const updatedTitle = payload.title + ' mutated'; |
| 145 | + const updatedContent = payload.content + ' mutated'; |
| 146 | + |
| 147 | + await exposed.controller.fetch( |
| 148 | + CoolerArticleResource.update, |
| 149 | + { id: payload.id }, |
| 150 | + { title: updatedTitle, content: updatedContent }, |
| 151 | + ); |
| 152 | + |
| 153 | + // Wait for re-render with new data |
| 154 | + await flushUntil(wrapper, () => wrapper.find('h3').text() === updatedTitle); |
| 155 | + |
| 156 | + expect(wrapper.find('h3').text()).toBe(updatedTitle); |
| 157 | + expect(wrapper.find('p').text()).toBe(updatedContent); |
| 158 | + }); |
| 159 | +}); |
0 commit comments