-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c55c27e
commit 296d606
Showing
6 changed files
with
300 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { AlpineRay } from '@/AlpineRay'; | ||
import { beforeEach, describe, expect, it } from 'vitest'; | ||
import { FakeAlpine } from '~tests/fakes/FakeAlpine'; | ||
|
||
// Fake Ray implementation | ||
class FakeRay { | ||
public tables: any[] = []; | ||
|
||
table(data: any) { | ||
this.tables.push(data); | ||
return this; | ||
} | ||
|
||
send(...args: any[]) { | ||
// No-op | ||
return this; | ||
} | ||
} | ||
|
||
describe('AlpineRay', () => { | ||
let alpineRay: AlpineRay; | ||
let fakeWindow: Window & { Alpine: FakeAlpine }; | ||
let fakeRayInstance: FakeRay; | ||
let fakeAlpine: FakeAlpine; | ||
|
||
beforeEach(() => { | ||
fakeAlpine = new FakeAlpine(); | ||
fakeWindow = { Alpine: fakeAlpine } as any; | ||
fakeRayInstance = new FakeRay(); | ||
|
||
alpineRay = AlpineRay.create() as AlpineRay; | ||
alpineRay.init(fakeRayInstance, fakeWindow); | ||
}); | ||
|
||
it('should initialize with given rayInstance and window', () => { | ||
expect(alpineRay.rayInstance).toBe(fakeRayInstance); | ||
expect(alpineRay.window).toBe(fakeWindow); | ||
}); | ||
|
||
it('should watch an Alpine store and send updates to Ray', () => { | ||
const storeName = 'testStore'; | ||
fakeAlpine.store(storeName, { value: 1 }); | ||
|
||
alpineRay.watchStore(storeName); | ||
|
||
expect(alpineRay.trackRays.store[storeName]).toBe(fakeRayInstance); | ||
expect(fakeRayInstance.tables.length).toBe(1); | ||
expect(fakeRayInstance.tables[0]).toEqual({ value: 1 }); | ||
|
||
// Update the store and verify that Ray receives the update | ||
fakeAlpine.updateStore(storeName, { value: 2 }); | ||
expect(fakeRayInstance.tables.length).toBe(2); | ||
expect(fakeRayInstance.tables[1]).toEqual({ value: 1 }); | ||
}); | ||
|
||
it('should unwatch an Alpine store', () => { | ||
const storeName = 'testStore'; | ||
fakeAlpine.store(storeName, { value: 1 }); | ||
|
||
alpineRay.watchStore(storeName); | ||
expect(alpineRay.trackRays.store[storeName]).toBe(fakeRayInstance); | ||
|
||
alpineRay.unwatchStore(storeName); | ||
expect(alpineRay.trackRays.store[storeName]).toBeUndefined(); | ||
|
||
// Updating the store should not send updates to Ray | ||
fakeAlpine.updateStore(storeName, { value: 2 }); | ||
expect(fakeRayInstance.tables.length).toBe(1); // No new table entries | ||
}); | ||
|
||
it('should handle multiple stores independently', () => { | ||
const storeName1 = 'store1'; | ||
const storeName2 = 'store2'; | ||
fakeAlpine.store(storeName1, { data: 'foo' }); | ||
fakeAlpine.store(storeName2, { data: 'bar' }); | ||
|
||
alpineRay.watchStore(storeName1); | ||
alpineRay.watchStore(storeName2); | ||
|
||
expect(fakeRayInstance.tables.length).toBe(2); | ||
expect(fakeRayInstance.tables[0]).toEqual({ data: 'foo' }); | ||
expect(fakeRayInstance.tables[1]).toEqual({ data: 'bar' }); | ||
|
||
// fakeAlpine.updateStore(storeName1, { data: 'updated foo' }); | ||
// console.log(fakeRayInstance.tables); | ||
// expect(fakeRayInstance.tables.length).toBe(4); | ||
// expect(fakeRayInstance.tables[2]).toEqual({ data: 'updated foo' }); | ||
|
||
// fakeAlpine.updateStore(storeName2, { data: 'updated bar' }); | ||
// expect(fakeRayInstance.tables.length).toBe(6); | ||
// expect(fakeRayInstance.tables[3]).toEqual({ data: 'updated bar' }); | ||
}); | ||
|
||
it('should not fail when unwatching a store that was not watched', () => { | ||
expect(() => { | ||
alpineRay.unwatchStore('nonExistentStore'); | ||
}).not.toThrow(); | ||
}); | ||
|
||
// it('should initialize with default instances if none provided', () => { | ||
// const defaultAlpineRay = new AlpineRay(); | ||
// defaultAlpineRay.init(); | ||
|
||
// expect(defaultAlpineRay.rayInstance).toBeDefined(); | ||
// expect(defaultAlpineRay.window).toBeDefined(); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`logs custom component events 1`] = `[]`; | ||
|
||
exports[`should initialize custom event listeners when logEvents is defined 1`] = ` | ||
{ | ||
"args": [ | ||
[ | ||
{ | ||
"event": "custom-event", | ||
"payload": { | ||
"foo": "bar", | ||
}, | ||
}, | ||
"alpine.js", | ||
], | ||
], | ||
"type": "table", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Fake Alpine.js implementation | ||
export class FakeAlpine { | ||
private stores: Record<string, any> = {}; | ||
private effects: Function[] = []; | ||
|
||
store(name: string, data?: any) { | ||
if (data !== undefined) { | ||
this.stores[name] = data; | ||
} | ||
return this.stores[name]; | ||
} | ||
|
||
effect(fn: Function) { | ||
this.effects.push(fn); | ||
fn(); | ||
} | ||
|
||
updateStore(name: string, data: any) { | ||
this.stores[name] = data; | ||
this.effects.forEach(effect => effect()); | ||
} | ||
} |