-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add tests for state container and utils. * Add staat tests * Start component test * Add react tests. * Update version. * Undo version changes
- Loading branch information
1 parent
6df8954
commit 289ef9e
Showing
12 changed files
with
343 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
module.exports = { | ||
"collectCoverageFrom": [ | ||
"packages/**/*.(ts|tsx)", | ||
"!packages/**/*.d.(ts|tsx)", | ||
"!<rootDir>/node_modules/", | ||
"!<rootDir>/**/build/**/*.*", | ||
"!<rootDir>/packages/example/**/*.*", | ||
collectCoverageFrom: [ | ||
'packages/**/*.(ts|tsx)', | ||
'!packages/**/*.d.(ts|tsx)', | ||
'!packages/**/index.(ts|tsx)', | ||
'!<rootDir>/node_modules/', | ||
'!<rootDir>/**/build/**/*.*', | ||
'!<rootDir>/packages/example/**/*.*', | ||
], | ||
"roots": [ | ||
"<rootDir>/packages" | ||
], | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest" | ||
roots: ['<rootDir>/packages'], | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
"testRegex": "(/__tests__/.*\.test)\.tsx?$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
], | ||
} | ||
testRegex: '(/__tests__/.*.test).tsx?$', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
}; |
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,90 @@ | ||
import staat from '../staat'; | ||
import { Staat } from '../types'; | ||
|
||
type TestState = { | ||
count: number; | ||
}; | ||
|
||
const state: TestState = { | ||
count: 0, | ||
}; | ||
|
||
const transformers = { | ||
add(currentState: TestState, value: number) { | ||
return { ...currentState, count: currentState.count + value }; | ||
}, | ||
subtract(currentState: TestState, value: number) { | ||
return { ...currentState, count: currentState.count - value }; | ||
}, | ||
}; | ||
|
||
describe('staat', () => { | ||
let sut: Staat<TestState, typeof transformers>; | ||
beforeEach(() => { | ||
sut = staat(transformers, state); | ||
}); | ||
|
||
it('sets up all members', () => { | ||
expect(sut).toHaveProperty('currentState'); | ||
expect(sut.currentState).toBe(state); | ||
expect(typeof sut.subscribe).toBe('function'); | ||
expect(typeof sut.unsubscribe).toBe('function'); | ||
expect(typeof sut.add).toBe('function'); | ||
expect(typeof sut.subtract).toBe('function'); | ||
}); | ||
|
||
it('sets up deep transformers', () => { | ||
const sut2 = staat({ deep: transformers }, state); | ||
expect(sut2).toHaveProperty('deep'); | ||
expect(typeof sut2.deep.add).toBe('function'); | ||
expect(typeof sut2.deep.subtract).toBe('function'); | ||
}); | ||
|
||
it('creates external trasformers signature and calls the declaration', async () => { | ||
let newState = await sut.add(100); | ||
expect(sut.currentState).toEqual({ count: 100 }); | ||
expect(newState).toEqual({ count: 100 }); | ||
expect(sut.currentState).not.toBe(state); | ||
newState = await sut.subtract(50); | ||
expect(sut.currentState).toEqual({ count: 50 }); | ||
expect(newState).toEqual({ count: 50 }); | ||
expect(sut.currentState).not.toBe(state); | ||
}); | ||
|
||
it('fires subscription when calling transformer', async () => { | ||
const subscription = jest.fn(); | ||
sut.subscribe(subscription); | ||
await sut.add(1); | ||
expect(subscription).toHaveBeenCalled(); | ||
}); | ||
|
||
it('unsubscribes from state', async () => { | ||
const subscription = jest.fn(); | ||
sut.subscribe(subscription); | ||
await sut.add(1); | ||
sut.unsubscribe(subscription); | ||
await sut.add(1); | ||
expect(subscription).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('calls asynchronous transformers', async () => { | ||
const sut2 = staat( | ||
{ | ||
multiply(currentState: TestState, value: number) { | ||
return Promise.resolve({ | ||
...currentState, | ||
count: currentState.count * value, | ||
}); | ||
}, | ||
}, | ||
{ | ||
count: 2, | ||
}, | ||
); | ||
|
||
const newState = await sut2.multiply(10); | ||
|
||
expect(sut2.currentState).toEqual({ count: 20 }); | ||
expect(newState).toEqual({ count: 20 }); | ||
}); | ||
}); |
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,5 +1,58 @@ | ||
import { StateContainer } from '../state-container'; | ||
|
||
type TestState = { | ||
count: number; | ||
}; | ||
|
||
const state: TestState = { | ||
count: 0, | ||
}; | ||
|
||
describe('StateContainer', () => { | ||
it('should test', () => { | ||
expect(true).toBe(true); | ||
let sut: StateContainer<TestState>; | ||
beforeEach(() => { | ||
sut = new StateContainer(state); | ||
}); | ||
|
||
it('returns initial state', () => { | ||
expect(sut.getState()).toBe(state); | ||
}); | ||
|
||
it('sets new state', async () => { | ||
const newState = { ...state, count: 1 }; | ||
await sut.setState(newState); | ||
expect(sut.getState()).toEqual(newState); | ||
expect(sut).not.toBe(state); | ||
}); | ||
|
||
it('fires subscription when setting state', async () => { | ||
const subscriptionMock = jest.fn(); | ||
sut.subscribe(subscriptionMock); | ||
const newState = { ...state, count: 1 }; | ||
await sut.setState(newState); | ||
expect(subscriptionMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('fires multiple subscriptions when setting state', async () => { | ||
const subscriptionMock1 = jest.fn(); | ||
const subscriptionMock2 = jest.fn(); | ||
sut.subscribe(subscriptionMock1); | ||
sut.subscribe(subscriptionMock2); | ||
const newState = { ...state, count: 1 }; | ||
await sut.setState(newState); | ||
expect(subscriptionMock1).toHaveBeenCalled(); | ||
expect(subscriptionMock2).toHaveBeenCalled(); | ||
}); | ||
|
||
it('does not fire multiple subscriptions when unsubscribed', async () => { | ||
const subscriptionMock1 = jest.fn(); | ||
const subscriptionMock2 = jest.fn(); | ||
sut.subscribe(subscriptionMock1); | ||
sut.subscribe(subscriptionMock2); | ||
const newState = { ...state, count: 1 }; | ||
sut.unsubscribe(subscriptionMock1); | ||
await sut.setState(newState); | ||
expect(subscriptionMock1).not.toHaveBeenCalled(); | ||
expect(subscriptionMock2).toHaveBeenCalled(); | ||
}); | ||
}); |
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
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,5 +1,94 @@ | ||
import React from 'react'; | ||
import { reactStaat } from '../react'; | ||
import staat from 'staat'; | ||
import { ReactStaat } from '../types'; | ||
import { render, fireEvent } from 'react-testing-library'; | ||
/* tslint:disable no-submodule-imports */ | ||
import 'jest-dom/extend-expect'; | ||
|
||
type TestState = { | ||
count: number; | ||
}; | ||
|
||
const state: TestState = { | ||
count: 0, | ||
}; | ||
|
||
const transformers = { | ||
add(currentState: TestState, value: number) { | ||
return { ...currentState, count: currentState.count + value }; | ||
}, | ||
subtract(currentState: TestState, value: number) { | ||
return { ...currentState, count: currentState.count - value }; | ||
}, | ||
}; | ||
|
||
type OwnProps = {}; | ||
type TransformerProps = { | ||
add(value: number): TestState | Promise<TestState>; | ||
}; | ||
type TestComponentProps = TestState & OwnProps & TransformerProps; | ||
|
||
const TestComponent: React.StatelessComponent<TestComponentProps> = ({ | ||
count, | ||
add, | ||
}) => { | ||
return ( | ||
<React.Fragment> | ||
<div>Count: {count}</div> | ||
<button onClick={() => add(10)}>Add</button> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
describe('React', () => { | ||
it('should test', () => { | ||
expect(true).toBe(true); | ||
let sut: ReactStaat<TestState>; | ||
let ConnectedComponent: React.ComponentType<OwnProps>; | ||
|
||
beforeEach(() => { | ||
const staatState = staat(transformers, state); | ||
sut = reactStaat(staatState); | ||
|
||
ConnectedComponent = sut.connect<OwnProps, TestState, TransformerProps>( | ||
({ count }) => { | ||
return { | ||
count, | ||
}; | ||
}, | ||
() => ({ | ||
add: staatState.add, | ||
}), | ||
)(TestComponent); | ||
}); | ||
|
||
it('builds react-staat object', () => { | ||
expect(typeof sut.Provider).toBe('function'); | ||
expect(typeof sut.connect).toBe('function'); | ||
}); | ||
|
||
it('should render component', () => { | ||
const tree = ( | ||
<sut.Provider> | ||
<ConnectedComponent /> | ||
</sut.Provider> | ||
); | ||
const { getByText } = render(tree); | ||
expect(getByText(/^Count:/).textContent).toBe('Count: 0'); | ||
}); | ||
|
||
it('should update component', async () => { | ||
const tree = ( | ||
<sut.Provider> | ||
<ConnectedComponent /> | ||
</sut.Provider> | ||
); | ||
const { getByText, rerender } = render(tree); | ||
await fireEvent.click(getByText('Add')); | ||
await rerender( | ||
<sut.Provider> | ||
<ConnectedComponent /> | ||
</sut.Provider>, | ||
); | ||
expect(getByText(/^Count:/).textContent).toBe('Count: 10'); | ||
}); | ||
}); |
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,5 @@ | ||
import { reactStaat } from './react'; | ||
|
||
export * from 'staat'; | ||
|
||
export default reactStaat; |
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
Oops, something went wrong.