-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from mjolnirjs/release/0.1.0
WIP: add test units
- Loading branch information
Showing
15 changed files
with
698 additions
and
44 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 |
---|---|---|
|
@@ -7,5 +7,6 @@ node_modules | |
.rts2_cache_umd | ||
dist | ||
|
||
coverage | ||
# local history | ||
.history |
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,20 @@ | ||
import * as React from 'react'; | ||
import { | ||
StatedBeanContextValue, | ||
getStatedBeanContext, | ||
} from './StatedBeanContext'; | ||
|
||
export interface StatedBeanConsumerProps { | ||
children: (context: StatedBeanContextValue) => React.ReactNode; | ||
} | ||
|
||
export const StatedBeanConsumer: React.FC<StatedBeanConsumerProps> = ({ | ||
children, | ||
}) => { | ||
const StatedBeanContext = getStatedBeanContext(); | ||
return ( | ||
<StatedBeanContext.Consumer> | ||
{context => children(context)} | ||
</StatedBeanContext.Consumer> | ||
); | ||
}; |
File renamed without changes.
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,2 +1,3 @@ | ||
export * from './StatedBeanContext'; | ||
export * from './StatedBeanProvider'; | ||
export * from './StatedBeanConsumer'; |
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,11 +1,10 @@ | ||
import { StatedBeanMetaStorage } from './storage'; | ||
|
||
export function getMetadataStorage(): StatedBeanMetaStorage { | ||
if (window.StateBeanMetadataStorage === undefined) { | ||
window.StateBeanMetadataStorage = new StatedBeanMetaStorage(); | ||
} | ||
|
||
return window.StateBeanMetadataStorage; | ||
return ( | ||
(<any>window).StateBeanMetadataStorage || | ||
((<any>window).StateBeanMetadataStorage = new StatedBeanMetaStorage()) | ||
); | ||
} | ||
|
||
export * from './storage'; |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`react provider StatedBeanProvider 1`] = ` | ||
Array [ | ||
<div> | ||
0 | ||
<button | ||
id="addBtn" | ||
onClick={[Function]} | ||
> | ||
add | ||
</button> | ||
</div>, | ||
<div> | ||
0 | ||
<button | ||
id="addBtn" | ||
onClick={[Function]} | ||
> | ||
add | ||
</button> | ||
</div>, | ||
] | ||
`; |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { getMetadataStorage } from '../src/metadata'; | ||
import { StatedBean, Stated } from '../src'; | ||
|
||
describe('metadata', () => { | ||
let smapleStatedBeanName: string; | ||
|
||
beforeAll(() => { | ||
getMetadataStorage().clear(); | ||
|
||
@StatedBean() | ||
class SampleStatedBean { | ||
@Stated() | ||
public statedField: number; | ||
|
||
@Stated() | ||
public statedField2: string; | ||
|
||
public constructor() { | ||
this.statedField = 0; | ||
this.statedField2 = 'testStatedField'; | ||
} | ||
} | ||
|
||
smapleStatedBeanName = SampleStatedBean.name; | ||
}); | ||
|
||
it('get metadata storage from window', () => { | ||
const storage = getMetadataStorage(); | ||
expect(storage).not.toBeNull(); | ||
}); | ||
|
||
it('stated bean decorator metadata collected', () => { | ||
const storage = getMetadataStorage(); | ||
const beanMeta = storage.getBeanMeta(smapleStatedBeanName); | ||
|
||
expect(beanMeta).not.toBeNull(); | ||
expect(beanMeta!.name).toEqual(smapleStatedBeanName); | ||
expect(beanMeta!.statedFields).not.toBeNull(); | ||
|
||
if (beanMeta !== undefined && beanMeta.statedFields !== undefined) { | ||
const field1 = beanMeta.statedFields[0]; | ||
expect(field1.name).toEqual('statedField'); | ||
} | ||
}); | ||
}); |
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,140 @@ | ||
import * as React from 'react'; | ||
import * as ReactDOM from 'react-dom'; | ||
import * as renderer from 'react-test-renderer'; | ||
import Enzyme, { mount } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
|
||
import { getMetadataStorage } from '../src/metadata'; | ||
import { | ||
StatedBean, | ||
Stated, | ||
StatedBeanConsumer, | ||
StatedBeanProvider, | ||
useStatedBean, | ||
StatedBeanContextValue, | ||
} from '../src'; | ||
import { ClassType } from '../src/types/ClassType'; | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
describe('react provider', () => { | ||
let TestStatedBean: ClassType; | ||
|
||
class T {} | ||
|
||
beforeAll(() => { | ||
getMetadataStorage().clear(); | ||
|
||
@StatedBean() | ||
class SampleStatedBean { | ||
@Stated() | ||
public statedField: number; | ||
|
||
@Stated() | ||
public statedField2: string; | ||
|
||
public constructor() { | ||
this.statedField = 0; | ||
this.statedField2 = 'testStatedField'; | ||
} | ||
|
||
addStatedField = () => { | ||
this.statedField += 1; | ||
}; | ||
} | ||
|
||
TestStatedBean = SampleStatedBean; | ||
}); | ||
|
||
it('StatedBeanProvider', () => { | ||
const Sample = () => { | ||
const bean = useStatedBean(TestStatedBean); | ||
|
||
try { | ||
expect(useStatedBean(T)).toThrow(); | ||
} catch (e) {} | ||
|
||
expect(bean).not.toBeNull(); | ||
expect(bean.statedField).toEqual(0); | ||
|
||
return ( | ||
<div> | ||
{bean.statedField}{' '} | ||
<button id="addBtn" onClick={bean.addStatedField}> | ||
add | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
return ( | ||
<StatedBeanProvider types={[TestStatedBean]}> | ||
<Sample /> | ||
<StatedBeanProvider types={[TestStatedBean]}> | ||
<Sample /> | ||
</StatedBeanProvider> | ||
</StatedBeanProvider> | ||
); | ||
}; | ||
|
||
const app = renderer.create(<App />); | ||
let tree = app.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
|
||
it('useStatedBean and change the stated field', () => { | ||
const Sample = () => { | ||
console.log('Sample useStatedBean'); | ||
const bean = useStatedBean(TestStatedBean); | ||
return ( | ||
<div> | ||
<span className="field">field={bean.statedField}</span> | ||
<button onClick={bean.addStatedField}>add</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
return ( | ||
<StatedBeanProvider types={[TestStatedBean]}> | ||
<Sample /> | ||
</StatedBeanProvider> | ||
); | ||
}; | ||
|
||
const app = mount(<App />); | ||
|
||
expect(app.html().includes('field=0')).toBe(true); | ||
const sample = app.find('Sample'); | ||
sample.find('button').simulate('click'); | ||
expect(app.html().includes('field=1')).toBe(true); | ||
}); | ||
|
||
it('StatedBeanConsumer', () => { | ||
const Sample = () => { | ||
return ( | ||
<StatedBeanConsumer> | ||
{(context: StatedBeanContextValue) => { | ||
expect(context).not.toBeNull(); | ||
expect(context.container).not.toBeNull(); | ||
|
||
return null; | ||
}} | ||
</StatedBeanConsumer> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
return ( | ||
<StatedBeanProvider types={[TestStatedBean]}> | ||
<Sample /> | ||
</StatedBeanProvider> | ||
); | ||
}; | ||
|
||
const div = document.createElement('div'); | ||
ReactDOM.render(<App />, div); | ||
ReactDOM.unmountComponentAtNode(div); | ||
}); | ||
}); |
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.