-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enhance useStore to provide direct access to store instances an…
…d values, update TypeScript types, and add corresponding tests
- Loading branch information
Showing
5 changed files
with
99 additions
and
31 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,9 @@ | ||
import type { Controller } from "@hotwired/stimulus" | ||
import type { Store } from './store'; | ||
|
||
export interface StoreController extends Controller { | ||
[key: string]: any; | ||
constructor: { | ||
stores?: Store<any>[]; | ||
}; | ||
} |
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,31 +1,72 @@ | ||
import { Store } from '../src/store'; | ||
import { useStore } from '../src/useStore'; | ||
import type { StoreController } from '../src/storeController'; | ||
|
||
describe('useStore', () => { | ||
it('should subscribe to stores and call update methods on value changes', () => { | ||
const mockController = { | ||
let mockController: StoreController; | ||
let testStore: Store<number>; | ||
|
||
beforeEach(() => { | ||
testStore = new Store('TestStore', 0); | ||
mockController = { | ||
constructor: { | ||
stores: [testStore] | ||
}, | ||
onTestStoreUpdate: jest.fn(), | ||
disconnect: jest.fn(), | ||
}; | ||
context: jest.fn(), | ||
application: jest.fn(), | ||
scope: jest.fn(), | ||
element: jest.fn(), | ||
// Add the other missing properties here... | ||
} as unknown as StoreController; | ||
}); | ||
|
||
const testStore = new Store('TestStore', 0); | ||
useStore(mockController, [testStore]); | ||
it('should subscribe to stores and call update methods on value changes', () => { | ||
useStore(mockController); | ||
|
||
testStore.set(5); | ||
expect(mockController.onTestStoreUpdate).toHaveBeenCalledWith(5); | ||
}); | ||
|
||
it('should clean up subscriptions when controller disconnects', () => { | ||
const mockController = { | ||
onTestStoreUpdate: jest.fn(), | ||
disconnect: jest.fn(), | ||
}; | ||
it('should allow direct access to store values on the controller', () => { | ||
useStore(mockController); | ||
|
||
testStore.set(10); | ||
expect(mockController.TestStoreValue).toBe(10); | ||
}); | ||
|
||
const testStore = new Store('TestStore', 0); | ||
useStore(mockController, [testStore]); | ||
it('should allow direct access to store instances on the controller', () => { | ||
useStore(mockController); | ||
|
||
expect(mockController.TestStore).toBe(testStore); | ||
}); | ||
|
||
it('should clean up subscriptions when controller disconnects', () => { | ||
useStore(mockController); | ||
|
||
const unsubscribe = jest.spyOn(testStore, 'unsubscribe'); | ||
mockController.disconnect(); | ||
testStore.set(10); | ||
expect(mockController.onTestStoreUpdate).not.toHaveBeenCalledWith(10); | ||
expect(unsubscribe).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should add a getter for the store value to the controller', () => { | ||
useStore(mockController); | ||
|
||
testStore.set(7); | ||
expect(mockController.TestStoreValue).toBe(7); | ||
}); | ||
|
||
it('should add a getter for the store instance to the controller', () => { | ||
useStore(mockController); | ||
|
||
expect(mockController.TestStore).toBe(testStore); | ||
}); | ||
|
||
it('should add an update method to the controller', () => { | ||
useStore(mockController); | ||
|
||
testStore.set(8); | ||
expect(mockController.onTestStoreUpdate).toHaveBeenCalledWith(8); | ||
}); | ||
}); |