-
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.
- Loading branch information
Showing
6 changed files
with
663 additions
and
535 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
src/packages/storybook/withStore/__tests__/withStore.spec.js
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,55 @@ | ||
import { StoreInjector } from '../withStore'; | ||
|
||
jest.mock('@storybook/addons', () => ({ | ||
getChannel: () => ({ | ||
on: () => {}, | ||
emit: () => {}, | ||
}), | ||
})); | ||
|
||
let withStore; | ||
|
||
describe('Storybook|injectStore decorator', () => { | ||
beforeEach(() => { | ||
withStore = injectedStore => new StoreInjector().withStore(injectedStore); | ||
}); | ||
|
||
it('should expos the puplic API', () => { | ||
const instance = withStore(); | ||
expect(typeof instance.getStore).toBe('function'); | ||
expect(typeof instance.resetStore).toBe('function'); | ||
}); | ||
|
||
it('should return stored values', () => { | ||
const instance = withStore({ | ||
string: 'test', | ||
object: { a: 1 }, | ||
array: [1, 2, 3], | ||
}); | ||
expect(instance.getStore().get('string')).toBe('test'); | ||
expect(instance.getStore().get('object')).toEqual({ a: 1 }); | ||
expect(instance.getStore().get('array')).toEqual([1, 2, 3]); | ||
}); | ||
|
||
it('should change just changed values', () => { | ||
const instance = withStore({ | ||
string: 'test', | ||
number: 1, | ||
}); | ||
instance.getStore().set({ string: 'new value' }); | ||
expect(instance.getStore().get('string')).toBe('new value'); | ||
expect(instance.getStore().get('number')).toBe(1); | ||
}); | ||
|
||
it('should reset store to initialValues', () => { | ||
const instance = withStore({ | ||
string: 'test', | ||
number: 1, | ||
}); | ||
instance.getStore().set({ string: 'new value' }); | ||
instance.getStore().set({ number: 2 }); | ||
instance.resetStore(); | ||
expect(instance.getStore().get('string')).toBe('test'); | ||
expect(instance.getStore().get('number')).toBe(1); | ||
}); | ||
}); |
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 @@ | ||
export { default, StoreInjector } from './withStore'; |
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,60 @@ | ||
import addons from '@storybook/addons'; // eslint-disable-line import/no-extraneous-dependencies | ||
import { STORY_CHANGED, FORCE_RE_RENDER } from '@storybook/core-events'; // eslint-disable-line import/no-extraneous-dependencies | ||
import { Store } from '@sambego/storybook-state'; // eslint-disable-line import/no-extraneous-dependencies | ||
|
||
/** | ||
* This decorator allows you to add store in Storybook | ||
* For inject store use Storybook API `addParameters` | ||
* Example: `.addParameters(injectStore({ param: 'one' }))` | ||
*/ | ||
class StoreInjector { | ||
/** | ||
* Used for keep store | ||
*/ | ||
globalStore = {}; | ||
|
||
/** | ||
* Used for keep initial store | ||
*/ | ||
initialStore = {}; | ||
|
||
constructor() { | ||
const channel = addons.getChannel(); | ||
// Fired when page changed | ||
channel.on(STORY_CHANGED, () => { | ||
this.createStore(this.initialStore); | ||
}); | ||
} | ||
|
||
/** | ||
* Used for create store | ||
*/ | ||
createStore(injectedStore) { | ||
this.globalStore = new Store(injectedStore); | ||
this.globalStore.subscribe(state => { | ||
// Enforce rerender stories when store was changed | ||
addons.getChannel().emit(FORCE_RE_RENDER); | ||
return state; | ||
}); | ||
addons.getChannel().emit(FORCE_RE_RENDER); | ||
} | ||
|
||
/** | ||
* Used for injected store in Storybook | ||
*/ | ||
withStore(injectedStore) { | ||
this.initialStore = injectedStore; | ||
this.createStore(injectedStore); | ||
return { | ||
getStore: () => this.globalStore, | ||
resetStore: () => this.createStore(injectedStore), | ||
}; | ||
} | ||
} | ||
|
||
const storeInjector = new StoreInjector(); | ||
|
||
export { StoreInjector }; | ||
export default function(injectedStore) { | ||
return storeInjector.withStore(injectedStore); | ||
} |
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