mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-09 00:19:13 +08:00
ADD 'getPropsMap' and 'getRendererFrom' tests
This commit is contained in:
parent
428c232381
commit
c5466a798c
82
addons/contexts/src/preview/libs/getPropsMap.test.ts
Normal file
82
addons/contexts/src/preview/libs/getPropsMap.test.ts
Normal file
@ -0,0 +1,82 @@
|
||||
import { _getPropsByParamName, getPropsMap } from './getPropsMap';
|
||||
import { OPT_OUT } from '../../constants';
|
||||
|
||||
describe('Test on behaviors from collecting the propsMap', () => {
|
||||
const someParams = [{ name: 'A', props: {} }, { name: 'B', props: {} }];
|
||||
|
||||
it('should return "null" when params in 0 length', () => {
|
||||
const result = _getPropsByParamName([]);
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
|
||||
it('should return "OPT_OUT" token when the context being opted out', () => {
|
||||
const result = _getPropsByParamName(someParams, OPT_OUT);
|
||||
expect(result).toBe(OPT_OUT);
|
||||
});
|
||||
|
||||
it('should return the props from params when the name existed', () => {
|
||||
const target = {};
|
||||
const result = _getPropsByParamName([...someParams, { name: 'C', props: target }], 'C');
|
||||
expect(result).toBe(target);
|
||||
});
|
||||
|
||||
it('should otherwise fallback to default props in params for a bad name', () => {
|
||||
const target = {};
|
||||
const result = _getPropsByParamName(
|
||||
[...someParams, { name: 'C', props: target, default: true }],
|
||||
'X'
|
||||
);
|
||||
expect(result).toBe(target);
|
||||
});
|
||||
|
||||
it('should otherwise fallback to the first props in params for a bad name, if no marked default props', () => {
|
||||
const result = _getPropsByParamName(someParams, 'A');
|
||||
expect(result).toBe(someParams[0].props);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Test on the integrity of the method to get the propMaps', () => {
|
||||
it('should return the correct propsMap from the specified selectionState', () => {
|
||||
// setup
|
||||
const someContextNodes = [
|
||||
{
|
||||
components: ['div'],
|
||||
icon: 'box' as const,
|
||||
nodeId: 'Some Context',
|
||||
options: { cancelable: false, deep: false, disable: false },
|
||||
params: [{ name: 'A1', props: { a: 1 } }, { name: 'A2', props: { a: 2 }, default: true }],
|
||||
title: 'Some Context',
|
||||
},
|
||||
{
|
||||
components: ['div'],
|
||||
icon: 'box' as const,
|
||||
nodeId: 'Another Context',
|
||||
options: { cancelable: false, deep: false, disable: false },
|
||||
params: [{ name: 'B', props: { b: 1 } }],
|
||||
title: 'Another Context',
|
||||
},
|
||||
{
|
||||
components: ['span'],
|
||||
icon: 'box' as const,
|
||||
nodeId: 'Other Contexts',
|
||||
options: { cancelable: false, deep: false, disable: false },
|
||||
params: [{ name: 'C', props: { c: 1 } }],
|
||||
title: 'Other Contexts',
|
||||
},
|
||||
];
|
||||
const someSelectionState = {
|
||||
'Some Context': 'A1',
|
||||
'Another Context': OPT_OUT,
|
||||
};
|
||||
|
||||
// exercise
|
||||
const result = getPropsMap(someContextNodes, someSelectionState);
|
||||
|
||||
// assertion
|
||||
expect(result).toEqual({
|
||||
'Some Context': { a: 1 },
|
||||
'Another Context': OPT_OUT,
|
||||
'Other Contexts': { c: 1 },
|
||||
});
|
||||
});
|
||||
});
|
90
addons/contexts/src/preview/libs/getRendererFrom.test.ts
Normal file
90
addons/contexts/src/preview/libs/getRendererFrom.test.ts
Normal file
@ -0,0 +1,90 @@
|
||||
import { _getAggregatedWrap, getRendererFrom } from './getRendererFrom';
|
||||
import { OPT_OUT } from '../../constants';
|
||||
|
||||
// mocks
|
||||
const h = jest.fn();
|
||||
const spiedAggregator = _getAggregatedWrap(h);
|
||||
|
||||
beforeEach(() => {
|
||||
h.mockReset();
|
||||
});
|
||||
|
||||
// tests
|
||||
describe('Test on aggregation of a single context', () => {
|
||||
const fakeTag = 'fakeTag';
|
||||
const fakeComponent = () => '';
|
||||
|
||||
it('should skip wrapping when being set to disable', () => {
|
||||
const testedProps = {};
|
||||
const testedOption = { disable: true };
|
||||
spiedAggregator([fakeTag, fakeComponent], testedProps, testedOption)();
|
||||
expect(h).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
it('should skip wrapping when props is marked as "OPT_OUT"', () => {
|
||||
const testedProps = OPT_OUT;
|
||||
const testedOption = {};
|
||||
spiedAggregator([fakeTag, fakeComponent], testedProps, testedOption)();
|
||||
expect(h).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
it('should wrap components in the stacking order', () => {
|
||||
const testedProps = {};
|
||||
const testedOption = {};
|
||||
spiedAggregator([fakeTag, fakeComponent], testedProps, testedOption)();
|
||||
expect(h).toBeCalledTimes(2);
|
||||
expect(h.mock.calls[0][0]).toBe(fakeComponent);
|
||||
expect(h.mock.calls[1][0]).toBe(fakeTag);
|
||||
});
|
||||
|
||||
it('should NOT pass props deeply by default', () => {
|
||||
const testedProps = {};
|
||||
const testedOption = {};
|
||||
spiedAggregator([fakeTag, fakeComponent], testedProps, testedOption)();
|
||||
expect(h.mock.calls[0][1]).toBe(null);
|
||||
expect(h.mock.calls[1][1]).toBe(testedProps);
|
||||
});
|
||||
|
||||
it('should pass props deeply', () => {
|
||||
const testedProps = {};
|
||||
const testedOption = { deep: true };
|
||||
spiedAggregator([fakeTag, fakeComponent], testedProps, testedOption)();
|
||||
expect(h.mock.calls[0][1]).toBe(testedProps);
|
||||
expect(h.mock.calls[1][1]).toBe(testedProps);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Test on aggregation of contexts', () => {
|
||||
it('should aggregate contexts in the stacking order', () => {
|
||||
// setup
|
||||
const someContextNodes = [
|
||||
{
|
||||
components: ['div'],
|
||||
icon: 'box' as const,
|
||||
nodeId: 'Some Context',
|
||||
options: { cancelable: false, deep: false, disable: false },
|
||||
params: [{ name: 'A', props: {} }],
|
||||
title: 'Some Context',
|
||||
},
|
||||
{
|
||||
components: ['span'],
|
||||
icon: 'box' as const,
|
||||
nodeId: 'Another Context',
|
||||
options: { cancelable: false, deep: false, disable: false },
|
||||
params: [{ name: 'B', props: {} }],
|
||||
title: 'Another Context',
|
||||
},
|
||||
];
|
||||
const propsMap = {
|
||||
'Some Context': {},
|
||||
'Another Context': {},
|
||||
};
|
||||
|
||||
// exercise
|
||||
getRendererFrom(h)(someContextNodes, propsMap, () => {});
|
||||
|
||||
// assertion
|
||||
expect(h.mock.calls[0][0]).toBe(someContextNodes[1].components[0]);
|
||||
expect(h.mock.calls[1][0]).toBe(someContextNodes[0].components[0]);
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user