mirror of
https://github.com/storybookjs/storybook.git
synced 2025-03-19 05:02:40 +08:00
rename/move class to objectType
This commit is contained in:
parent
415c34f48c
commit
5b9e213aff
@ -17,7 +17,7 @@ const input = {
|
||||
input.circular = input;
|
||||
|
||||
const output = {
|
||||
'$___storybook.className': 'Object',
|
||||
'$___storybook.objectName': 'Object',
|
||||
'$___storybook.isCyclic': true,
|
||||
a: 'A',
|
||||
b: 1,
|
||||
@ -30,7 +30,7 @@ const output = {
|
||||
},
|
||||
e: '2017-12-02T11:13:22.492Z',
|
||||
f: {
|
||||
'$___storybook.className': 'File',
|
||||
'$___storybook.objectName': 'File',
|
||||
isClosed: false,
|
||||
lastModified: 1512213202492,
|
||||
name: 'filename.txt',
|
||||
|
@ -2,7 +2,7 @@ import { getPropertiesList, typeReplacer } from './util';
|
||||
|
||||
import { CYCLIC_KEY } from './';
|
||||
|
||||
import { classType } from './types';
|
||||
import { objectType } from './types';
|
||||
|
||||
export default function decycle(object, depth = 10) {
|
||||
const objects = new WeakMap();
|
||||
@ -46,7 +46,7 @@ export default function decycle(object, depth = 10) {
|
||||
obj[i] = derez(value[i], `${path}[${i}]`, _depth + 1);
|
||||
}
|
||||
} else {
|
||||
obj = classType.serialize(value);
|
||||
obj = objectType.serialize(value);
|
||||
|
||||
getPropertiesList(value).forEach(name => {
|
||||
obj[name] = derez(value[name], `${path}[${JSON.stringify(name)}]`, _depth + 1);
|
||||
|
@ -1,14 +0,0 @@
|
||||
import createFakeConstructor from '../createFakeConstructor';
|
||||
|
||||
describe('createFakeConstructor', () => {
|
||||
it('creates fake constructor', () => {
|
||||
expect(
|
||||
createFakeConstructor(
|
||||
{
|
||||
name_key: 'A',
|
||||
},
|
||||
'name_key'
|
||||
).constructor.name
|
||||
).toEqual('A');
|
||||
});
|
||||
});
|
@ -1,26 +0,0 @@
|
||||
import getClassName from '../getClassName';
|
||||
|
||||
class A {}
|
||||
const a = new A();
|
||||
function B() {}
|
||||
const b = new B();
|
||||
|
||||
describe('getClassName', () => {
|
||||
/* Transpiled cannot be tested.
|
||||
it('get name of class',() => {
|
||||
expect(getClassName(A)).toBe('A')
|
||||
});
|
||||
*/
|
||||
|
||||
it('get name of class instance', () => {
|
||||
expect(getClassName(a)).toBe('A');
|
||||
});
|
||||
|
||||
it('get name of function', () => {
|
||||
expect(getClassName(B)).toBe('B');
|
||||
});
|
||||
|
||||
it('get constructor name', () => {
|
||||
expect(getClassName(b)).toBe('B');
|
||||
});
|
||||
});
|
@ -1,19 +0,0 @@
|
||||
import classType from '../';
|
||||
|
||||
describe('Class', () => {
|
||||
it('Serializes Class', () => {
|
||||
class C {}
|
||||
const c = new C();
|
||||
|
||||
expect(classType.serialize(c)).toEqual({ [classType.KEY]: 'C' });
|
||||
});
|
||||
|
||||
it('Deserializes Class', () => {
|
||||
const value = { [classType.KEY]: 'C' };
|
||||
const c = classType.deserialize(value);
|
||||
|
||||
expect(c.constructor.name).toEqual('C');
|
||||
|
||||
expect(value).toEqual({});
|
||||
});
|
||||
});
|
@ -1,13 +0,0 @@
|
||||
import createFakeConstructor from './createFakeConstructor';
|
||||
import getClassName from './getClassName';
|
||||
|
||||
const KEY = '$___storybook.className';
|
||||
|
||||
const classType = {
|
||||
KEY,
|
||||
// is: (value) => , // not used
|
||||
serialize: value => ({ [KEY]: getClassName(value) }),
|
||||
deserialize: value => createFakeConstructor(value, KEY),
|
||||
};
|
||||
|
||||
export default classType;
|
@ -1,4 +1,4 @@
|
||||
import classType from './class';
|
||||
import objectType from './object';
|
||||
import dateType from './date';
|
||||
import functionType from './function';
|
||||
import infinityType from './infinity';
|
||||
@ -7,7 +7,7 @@ import regexpType from './regexp';
|
||||
import symbolType from './symbol';
|
||||
import undefinedType from './undefined';
|
||||
|
||||
export { classType };
|
||||
export { objectType };
|
||||
export { dateType };
|
||||
export { functionType };
|
||||
export { infinityType };
|
||||
|
@ -0,0 +1,14 @@
|
||||
import createNamedObject from '../createNamedObject';
|
||||
|
||||
describe('createNamedObject', () => {
|
||||
it('creates named object', () => {
|
||||
expect(
|
||||
createNamedObject(
|
||||
{
|
||||
name_key: 'A',
|
||||
},
|
||||
'name_key'
|
||||
).constructor.name
|
||||
).toEqual('A');
|
||||
});
|
||||
});
|
@ -0,0 +1,20 @@
|
||||
import getObjectName from '../getObjectName';
|
||||
|
||||
class A {}
|
||||
const a = new A();
|
||||
function B() {}
|
||||
const b = new B();
|
||||
|
||||
describe('getObjectName', () => {
|
||||
it('get name of instance', () => {
|
||||
expect(getObjectName(a)).toBe('A');
|
||||
});
|
||||
|
||||
it('get name of function', () => {
|
||||
expect(getObjectName(B)).toBe('B');
|
||||
});
|
||||
|
||||
it('get constructor name', () => {
|
||||
expect(getObjectName(b)).toBe('B');
|
||||
});
|
||||
});
|
19
addons/actions/src/lib/types/object/__tests__/index.js
Normal file
19
addons/actions/src/lib/types/object/__tests__/index.js
Normal file
@ -0,0 +1,19 @@
|
||||
import objectType from '../';
|
||||
|
||||
describe('Object', () => {
|
||||
it('Serializes Object', () => {
|
||||
function C() {}
|
||||
const c = new C();
|
||||
|
||||
expect(objectType.serialize(c)).toEqual({ [objectType.KEY]: 'C' });
|
||||
});
|
||||
|
||||
it('Deserializes Object', () => {
|
||||
const value = { [objectType.KEY]: 'C' };
|
||||
const c = objectType.deserialize(value);
|
||||
|
||||
expect(c.constructor.name).toEqual('C');
|
||||
|
||||
expect(value).toEqual({});
|
||||
});
|
||||
});
|
@ -1,12 +1,12 @@
|
||||
import createFunction from '../function/createFunction';
|
||||
|
||||
export default function createFakeConstructor(obj, key) {
|
||||
export default function createNamedObject(obj, key) {
|
||||
const Func = createFunction(obj[key]);
|
||||
const func = new Func();
|
||||
const namedObj = new Func();
|
||||
|
||||
delete obj[key]; // eslint-disable-line no-param-reassign
|
||||
|
||||
Object.assign(func, obj);
|
||||
Object.assign(namedObj, obj);
|
||||
|
||||
return func;
|
||||
return namedObj;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
export default function getClassName(value) {
|
||||
export default function getObjectName(value) {
|
||||
if (value.toString) {
|
||||
const stringValue = value.toString();
|
||||
|
13
addons/actions/src/lib/types/object/index.js
Normal file
13
addons/actions/src/lib/types/object/index.js
Normal file
@ -0,0 +1,13 @@
|
||||
import createNamedObject from './createNamedObject';
|
||||
import getObjectName from './getObjectName';
|
||||
|
||||
const KEY = '$___storybook.objectName';
|
||||
|
||||
const objectType = {
|
||||
KEY,
|
||||
// is: (value) => , // not used
|
||||
serialize: value => ({ [KEY]: getObjectName(value) }),
|
||||
deserialize: value => createNamedObject(value, KEY),
|
||||
};
|
||||
|
||||
export default objectType;
|
@ -1,6 +1,6 @@
|
||||
import typeReviver from '../typeReviver';
|
||||
import {
|
||||
classType,
|
||||
objectType,
|
||||
dateType,
|
||||
functionType,
|
||||
infinityType,
|
||||
@ -13,8 +13,8 @@ import {
|
||||
const date = '2017-12-02T11:13:22.492Z';
|
||||
|
||||
describe('typeReviver', () => {
|
||||
it('Revives className', () => {
|
||||
expect(typeReviver({ [classType.KEY]: 'C' }).value.constructor.name).toEqual('C');
|
||||
it('Revives object name', () => {
|
||||
expect(typeReviver({ [objectType.KEY]: 'C' }).value.constructor.name).toEqual('C');
|
||||
});
|
||||
it('Revives Date', () => {
|
||||
expect(typeReviver({ [dateType.KEY]: date })).toEqual({
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { types, classType } from '../types';
|
||||
import { types, objectType } from '../types';
|
||||
|
||||
const { hasOwnProperty } = Object.prototype;
|
||||
|
||||
const allTypes = types.concat(classType);
|
||||
const allTypes = types.concat(objectType);
|
||||
|
||||
function typeFilter(value) {
|
||||
const found = allTypes.find(type => hasOwnProperty.call(value, type.KEY));
|
||||
|
@ -50,7 +50,7 @@ describe('preview', () => {
|
||||
action('foo')(a);
|
||||
|
||||
expect(JSON.parse(channel.emit.mock.calls[0][1].data.args[0])).toEqual({
|
||||
'$___storybook.className': 'A',
|
||||
'$___storybook.objectName': 'A',
|
||||
a: 'b',
|
||||
});
|
||||
});
|
||||
@ -66,7 +66,7 @@ describe('preview', () => {
|
||||
action('foo')(a);
|
||||
|
||||
expect(JSON.parse(channel.emit.mock.calls[0][1].data.args[0])).toEqual({
|
||||
'$___storybook.className': 'A',
|
||||
'$___storybook.objectName': 'A',
|
||||
'$___storybook.isCyclic': true,
|
||||
a: {
|
||||
$ref: '$',
|
||||
|
Loading…
x
Reference in New Issue
Block a user