|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { QueryStore } from './QueryStore'; |
| 3 | +import util from 'util'; |
| 4 | + |
| 5 | +const tick = () => new Promise((r) => setTimeout(r, 100)); |
| 6 | + |
| 7 | +describe('QueryStore', () => { |
| 8 | + const mockExec = vi.fn(); |
| 9 | + const mockSubscription = vi.fn(); |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + vi.resetAllMocks(); |
| 13 | + mockExec.mockImplementation((q: string) => { |
| 14 | + if (q.startsWith('--col-metadata')) |
| 15 | + return Promise.resolve([{ column_name: 'x', column_type: 'INTEGER' }]); |
| 16 | + if (q.startsWith('--len')) return Promise.resolve([{ length: 5 }]); |
| 17 | + return Promise.resolve([{ x: 1 }, { x: 2 }, { x: 3 }, { x: 4 }, { x: 5 }]); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should be defined', () => { |
| 22 | + expect(QueryStore).toBeDefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should be subscribeable', () => { |
| 26 | + const store = new QueryStore('SELECT 1', mockExec); |
| 27 | + store.subscribe(mockSubscription); |
| 28 | + |
| 29 | + expect(mockSubscription).toHaveBeenCalledOnce(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should execute a query when accessing the .length property', async () => { |
| 33 | + const store = new QueryStore('SELECT 1', mockExec); |
| 34 | + store.subscribe(mockSubscription); |
| 35 | + |
| 36 | + // Accessing the property should be enough |
| 37 | + store.proxy.length; |
| 38 | + |
| 39 | + // let the event loop do its thing and the store hash things out |
| 40 | + await tick(); |
| 41 | + |
| 42 | + expect(store.proxy.length).toBe(5); |
| 43 | + |
| 44 | + // 1. Initial Subscription |
| 45 | + // 2. Length loading started |
| 46 | + // 3. Length loading finished |
| 47 | + // 4. Column Descriptions become available |
| 48 | + expect(mockSubscription).toHaveBeenCalledTimes(4); |
| 49 | + // 1. Column Metadata |
| 50 | + // 2. Length |
| 51 | + expect(mockExec).toHaveBeenCalledTimes(2); |
| 52 | + |
| 53 | + // Data was not touched |
| 54 | + // This query should not have fired |
| 55 | + expect(store.loaded).toBe(false); |
| 56 | + expect(store.loading).toBe(false); |
| 57 | + |
| 58 | + // Length should have finished loading |
| 59 | + expect(store.lengthLoaded).toBe(true); |
| 60 | + expect(store.lengthLoading).toBe(false); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should ensure that 2 queries with the same derivation are distinct', () => { |
| 64 | + const store1 = new QueryStore('SELECT 1;', mockExec, undefined, { disableCache: false }); |
| 65 | + const store2 = new QueryStore('SELECT 2;', mockExec, undefined, { disableCache: false }); |
| 66 | + |
| 67 | + const limitedStore1 = store1.limit(0); |
| 68 | + const limitedStore2 = store2.limit(0); |
| 69 | + |
| 70 | + expect(limitedStore1.text).not.toEqual(limitedStore2.text); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should slice properly', async () => { |
| 74 | + const store = new QueryStore('SELECT 2;', mockExec, undefined, { disableCache: false }).proxy; |
| 75 | + |
| 76 | + await store.fetch(); |
| 77 | + |
| 78 | + const sliced = store.slice(); |
| 79 | + |
| 80 | + console.error(sliced.length, store.length); |
| 81 | + expect(sliced.length).toEqual(store.length); |
| 82 | + |
| 83 | + expect(sliced[0]).toEqual(store.proxy[0]); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('Derived Stores', () => { |
| 87 | + describe.each<{ func: keyof QueryStore; args: unknown[] }>([ |
| 88 | + { func: 'where', args: [] }, |
| 89 | + { func: 'agg', args: [{}] }, |
| 90 | + { func: 'limit', args: [5] }, |
| 91 | + { func: 'orderBy', args: [{}] }, |
| 92 | + { func: 'offset', args: [] } |
| 93 | + ])('$func', (opts) => { |
| 94 | + it(`should have the property ${opts.func}()`, () => { |
| 95 | + const store = new QueryStore('SELECT 1;', mockExec, undefined, { disableCache: true }); |
| 96 | + expect(opts.func in store).toBe(true); |
| 97 | + expect(typeof store[opts.func]).toBe('function'); |
| 98 | + }); |
| 99 | + |
| 100 | + it(`should return a new store when using .${opts.func}`, () => { |
| 101 | + const store = new QueryStore('SELECT 1;', mockExec, undefined, { disableCache: true }); |
| 102 | + |
| 103 | + const targetFunc = store[opts.func]; |
| 104 | + expect(targetFunc).toBeTypeOf('function'); |
| 105 | + |
| 106 | + const childStore = (targetFunc as CallableFunction)(...opts.args); |
| 107 | + |
| 108 | + // store !== childStore |
| 109 | + expect(store).not.toEqual(childStore); |
| 110 | + // childStore is a proxy |
| 111 | + expect(util.types.isProxy(childStore)).toBe(true); |
| 112 | + }); |
| 113 | + it('should subscribe to derived stores', async () => { |
| 114 | + const store = new QueryStore('SELECT 1;', mockExec, undefined, { disableCache: true }); |
| 115 | + const targetFunc = store[opts.func]; |
| 116 | + expect(targetFunc).toBeTypeOf('function'); |
| 117 | + |
| 118 | + const childStore = (targetFunc as CallableFunction)(opts.args); |
| 119 | + store.subscribe(mockSubscription); |
| 120 | + |
| 121 | + childStore.length; |
| 122 | + |
| 123 | + await tick(); |
| 124 | + |
| 125 | + // 1. Parent Initial Load |
| 126 | + // 2. Parent Metadata |
| 127 | + // 3. Child Initial Load |
| 128 | + // 4. Child Metadata |
| 129 | + // 5. Child Length |
| 130 | + |
| 131 | + expect(mockSubscription).toBeCalledTimes(5); |
| 132 | + |
| 133 | + expect(childStore.length).toBe(5); |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments