|
| 1 | +import { createSchema, number, string, table, Zero } from '@rocicorp/zero' |
| 2 | +import { assert, describe, expect, it } from 'vitest' |
| 3 | +import { computed, createApp, inject, ref } from 'vue' |
| 4 | +import { createZero, zeroSymbol } from './create-zero' |
| 5 | + |
| 6 | +const testSchema = createSchema({ |
| 7 | + tables: [ |
| 8 | + table('test') |
| 9 | + .columns({ |
| 10 | + id: number(), |
| 11 | + name: string(), |
| 12 | + }) |
| 13 | + .primaryKey('id'), |
| 14 | + ], |
| 15 | +}) |
| 16 | + |
| 17 | +describe('createZero', () => { |
| 18 | + it('installs and provides zero instance to Vue app', () => { |
| 19 | + const app = createApp({}) |
| 20 | + app.use(createZero({ |
| 21 | + userID: 'test-user', |
| 22 | + server: null, |
| 23 | + schema: testSchema, |
| 24 | + kvStore: 'mem' as const, |
| 25 | + })) |
| 26 | + |
| 27 | + app.runWithContext(() => { |
| 28 | + const zero = inject(zeroSymbol) |
| 29 | + assert(zero?.value) |
| 30 | + expect(zero?.value.userID).toEqual('test-user') |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + it('accepts Zero instance instead of options', () => { |
| 35 | + const app = createApp({}) |
| 36 | + const zero = new Zero({ |
| 37 | + userID: 'test-user', |
| 38 | + server: null, |
| 39 | + schema: testSchema, |
| 40 | + kvStore: 'mem' as const, |
| 41 | + }) |
| 42 | + app.use(createZero({ zero })) |
| 43 | + |
| 44 | + app.runWithContext(() => { |
| 45 | + const injectedZero = inject(zeroSymbol) |
| 46 | + assert(injectedZero?.value) |
| 47 | + expect(injectedZero.value).toEqual(zero) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('updates when options change', async () => { |
| 52 | + const app = createApp({}) |
| 53 | + const userID = ref('test-user') |
| 54 | + const zeroOptions = computed(() => ({ |
| 55 | + userID: userID.value, |
| 56 | + server: null, |
| 57 | + schema: testSchema, |
| 58 | + kvStore: 'mem' as const, |
| 59 | + })) |
| 60 | + |
| 61 | + app.use(createZero(zeroOptions)) |
| 62 | + |
| 63 | + await app.runWithContext(async () => { |
| 64 | + const injectedZero = inject(zeroSymbol) |
| 65 | + assert(injectedZero?.value) |
| 66 | + |
| 67 | + expect(injectedZero.value.userID).toEqual('test-user') |
| 68 | + |
| 69 | + const oldZero = injectedZero.value |
| 70 | + |
| 71 | + userID.value = 'test-user-2' |
| 72 | + await 1 |
| 73 | + |
| 74 | + expect(injectedZero.value.userID).toEqual('test-user-2') |
| 75 | + expect(injectedZero.value.closed).toBe(false) |
| 76 | + expect(oldZero.closed).toBe(true) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('updates when Zero instance changes', async () => { |
| 81 | + const app = createApp({}) |
| 82 | + const userID = ref('test-user') |
| 83 | + |
| 84 | + const zero = computed(() => ({ zero: new Zero({ |
| 85 | + userID: userID.value, |
| 86 | + server: null, |
| 87 | + schema: testSchema, |
| 88 | + kvStore: 'mem' as const, |
| 89 | + }) })) |
| 90 | + |
| 91 | + app.use(createZero(zero)) |
| 92 | + |
| 93 | + await app.runWithContext(async () => { |
| 94 | + const injectedZero = inject(zeroSymbol) |
| 95 | + assert(injectedZero?.value) |
| 96 | + |
| 97 | + expect(injectedZero.value.userID).toEqual('test-user') |
| 98 | + |
| 99 | + const oldZero = injectedZero.value |
| 100 | + |
| 101 | + userID.value = 'test-user-2' |
| 102 | + await 1 |
| 103 | + |
| 104 | + expect(injectedZero.value.userID).toEqual('test-user-2') |
| 105 | + expect(injectedZero.value.closed).toBe(false) |
| 106 | + expect(oldZero.closed).toBe(true) |
| 107 | + }) |
| 108 | + }) |
| 109 | +}) |
0 commit comments