|
| 1 | +import { schema } from '@data-client/endpoint'; |
| 2 | +import { resource } from '@data-client/rest'; |
| 3 | +import { mount } from '@vue/test-utils'; |
| 4 | +import { defineComponent, h, nextTick } from 'vue'; |
| 5 | + |
| 6 | +import { |
| 7 | + ArticleWithSlug, |
| 8 | + ArticleSlugResource, |
| 9 | + ArticleResource, |
| 10 | + IDEntity, |
| 11 | +} from '../../../../__tests__/new'; |
| 12 | +import useQuery from '../consumers/useQuery'; |
| 13 | +import { provideDataClient } from '../providers/provideDataClient'; |
| 14 | + |
| 15 | +// Inline fixtures (duplicated from React tests to avoid cross-project imports) |
| 16 | +const payloadSlug = { |
| 17 | + id: 5, |
| 18 | + title: 'hi ho', |
| 19 | + slug: 'hi-ho', |
| 20 | + content: 'whatever', |
| 21 | + tags: ['a', 'best', 'react'], |
| 22 | +}; |
| 23 | +const nested = [ |
| 24 | + { |
| 25 | + id: 5, |
| 26 | + title: 'hi ho', |
| 27 | + content: 'whatever', |
| 28 | + tags: ['a', 'best', 'react'], |
| 29 | + author: { |
| 30 | + id: 23, |
| 31 | + username: 'bob', |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + id: 3, |
| 36 | + title: 'the next time', |
| 37 | + content: 'whatever', |
| 38 | + author: { |
| 39 | + id: 23, |
| 40 | + username: 'charles', |
| 41 | + |
| 42 | + }, |
| 43 | + }, |
| 44 | +]; |
| 45 | + |
| 46 | +describe('vue useQuery()', () => { |
| 47 | + async function flush() { |
| 48 | + await Promise.resolve(); |
| 49 | + await nextTick(); |
| 50 | + } |
| 51 | + |
| 52 | + const ProvideWrapper = defineComponent({ |
| 53 | + name: 'ProvideWrapper', |
| 54 | + setup(_props, { slots, expose }) { |
| 55 | + const { controller } = provideDataClient(); |
| 56 | + expose({ controller }); |
| 57 | + return () => (slots.default ? slots.default() : null); |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns undefined with empty state', async () => { |
| 62 | + const Inner = defineComponent({ |
| 63 | + setup() { |
| 64 | + const val = useQuery(ArticleWithSlug, { id: payloadSlug.id }); |
| 65 | + return () => h('div', (val.value as any)?.title || ''); |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + const wrapper = mount(ProvideWrapper, { |
| 70 | + slots: { default: () => h(Inner) }, |
| 71 | + }); |
| 72 | + await flush(); |
| 73 | + expect(wrapper.text()).toBe(''); |
| 74 | + }); |
| 75 | + |
| 76 | + it('finds Entity by pk/slug after setResponse', async () => { |
| 77 | + const Inner = defineComponent({ |
| 78 | + setup() { |
| 79 | + const byId = useQuery(ArticleWithSlug, { id: payloadSlug.id }); |
| 80 | + const bySlug = useQuery(ArticleWithSlug, { slug: payloadSlug.slug }); |
| 81 | + return () => |
| 82 | + h( |
| 83 | + 'div', |
| 84 | + `${(byId.value as any)?.title || ''}|${ |
| 85 | + (bySlug.value as any)?.title || '' |
| 86 | + }`, |
| 87 | + ); |
| 88 | + }, |
| 89 | + }); |
| 90 | + |
| 91 | + const wrapper = mount(ProvideWrapper, { |
| 92 | + slots: { default: () => h(Inner) }, |
| 93 | + }); |
| 94 | + const { controller }: any = wrapper.vm as any; |
| 95 | + |
| 96 | + // seed data via controller |
| 97 | + controller.setResponse( |
| 98 | + ArticleSlugResource.get, |
| 99 | + { id: payloadSlug.id }, |
| 100 | + payloadSlug, |
| 101 | + ); |
| 102 | + await flush(); |
| 103 | + |
| 104 | + expect(wrapper.text()).toContain(payloadSlug.title); |
| 105 | + }); |
| 106 | + |
| 107 | + it('selects Collections and updates when pushed', async () => { |
| 108 | + const ListComp = defineComponent({ |
| 109 | + setup() { |
| 110 | + const list = useQuery(ArticleResource.getList.schema); |
| 111 | + return () => |
| 112 | + h('div', (list.value || []).map((a: any) => a.id).join(',')); |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + const wrapper = mount(ProvideWrapper, { |
| 117 | + slots: { default: () => h(ListComp) }, |
| 118 | + }); |
| 119 | + const { controller }: any = wrapper.vm as any; |
| 120 | + |
| 121 | + controller.setResponse(ArticleResource.getList, {}, nested); |
| 122 | + await flush(); |
| 123 | + expect(wrapper.text().split(',').filter(Boolean).length).toBe( |
| 124 | + nested.length, |
| 125 | + ); |
| 126 | + |
| 127 | + // Simulate push by setting new list value |
| 128 | + const appended = nested.concat({ |
| 129 | + id: 50, |
| 130 | + title: 'new', |
| 131 | + content: 'x', |
| 132 | + } as any); |
| 133 | + controller.setResponse(ArticleResource.getList, {}, appended); |
| 134 | + await flush(); |
| 135 | + expect(wrapper.text().split(',').filter(Boolean).length).toBe( |
| 136 | + nested.length + 1, |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it('retrieves a nested collection (Collection of Array)', async () => { |
| 141 | + class Todo extends IDEntity { |
| 142 | + userId = 0; |
| 143 | + title = ''; |
| 144 | + completed = false; |
| 145 | + static key = 'Todo'; |
| 146 | + } |
| 147 | + |
| 148 | + class User extends IDEntity { |
| 149 | + name = ''; |
| 150 | + username = ''; |
| 151 | + email = ''; |
| 152 | + todos: Todo[] = []; |
| 153 | + static key = 'User'; |
| 154 | + static schema = { |
| 155 | + todos: new schema.Collection(new schema.Array(Todo), { |
| 156 | + nestKey: (parent: any) => ({ userId: parent.id }), |
| 157 | + }), |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + const userTodos = new schema.Collection(new schema.Array(Todo), { |
| 162 | + argsKey: ({ userId }: { userId: string }) => ({ userId }), |
| 163 | + }); |
| 164 | + |
| 165 | + const UserResource = resource({ schema: User, path: '/users/:id' }); |
| 166 | + |
| 167 | + const Inner = defineComponent({ |
| 168 | + setup() { |
| 169 | + const todos = useQuery(userTodos, { userId: '1' }); |
| 170 | + return () => h('div', (todos.value || []).length.toString()); |
| 171 | + }, |
| 172 | + }); |
| 173 | + |
| 174 | + const wrapper = mount(ProvideWrapper, { |
| 175 | + slots: { default: () => h(Inner) }, |
| 176 | + }); |
| 177 | + const { controller }: any = wrapper.vm as any; |
| 178 | + |
| 179 | + controller.setResponse( |
| 180 | + UserResource.get, |
| 181 | + { id: '1' }, |
| 182 | + { |
| 183 | + id: '1', |
| 184 | + todos: [{ id: '5', title: 'finish collections', userId: '1' }], |
| 185 | + username: 'bob', |
| 186 | + }, |
| 187 | + ); |
| 188 | + await flush(); |
| 189 | + |
| 190 | + expect(wrapper.text()).toBe('1'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('works with unions collections (sanity)', async () => { |
| 194 | + // Keep this light: verify we can call useQuery on a list schema and get an array |
| 195 | + const list = useQuery(ArticleResource.getList.schema); |
| 196 | + const Comp = defineComponent({ |
| 197 | + setup: () => () => h('div', (list.value || []).length), |
| 198 | + }); |
| 199 | + const wrapper = mount(ProvideWrapper, { |
| 200 | + slots: { default: () => h(Comp) }, |
| 201 | + }); |
| 202 | + const { controller }: any = wrapper.vm as any; |
| 203 | + controller.setResponse(ArticleResource.getList, {}, []); |
| 204 | + await flush(); |
| 205 | + expect(wrapper.text()).toBe('0'); |
| 206 | + }); |
| 207 | +}); |
0 commit comments