Skip to content

Commit

Permalink
fix: Fixed serializer and added proper tests (#33)
Browse files Browse the repository at this point in the history
* Fixed serializer and added proper tests

* Dependencies upgrade
  • Loading branch information
adriantoine authored Dec 7, 2016
1 parent 226c01c commit 309d6ea
Show file tree
Hide file tree
Showing 33 changed files with 3,154 additions and 29 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
node_modules
coverage
build
jsconfig.json
/serializer.js
.vscode
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"scripts": {
"build": "babel src -d build && babel src/serializer.js -o serializer.js",
"prepublish": "npm run build",
"test": "eslint src test && NODE_ENV=test jest --coverage",
"test": "eslint src test && npm run test-core && npm run test-serializer",
"test-core": "NODE_ENV=test jest --coverage",
"test-serializer": "cd tests/serializer && rm -rf node_modules && npm install && npm test",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"keywords": [
Expand Down Expand Up @@ -54,13 +56,19 @@
"cz-conventional-changelog": "^1.2.0",
"enzyme": "^2.4.1",
"eslint": "^3.4.0",
"eslint-plugin-babel": "^3.3.0",
"eslint-plugin-babel": "~4.0.0",
"eslint-plugin-react": "^6.2.0",
"jest": "^17.0.0",
"react": "^15.3.1",
"react-addons-test-utils": "^15.3.1",
"react-dom": "^15.3.1",
"semantic-release": "^4.3.5"
"semantic-release": "~6.3.2"
},
"jest": {
"testPathIgnorePatterns": [
"<rootDir>/tests/serializer",
"<rootDir>/node_modules/"
]
},
"config": {
"commitizen": {
Expand Down
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import ShallowWrapper from 'enzyme/build/ShallowWrapper';
import ReactWrapper from 'enzyme/build/ReactWrapper';

import {isShallowWrapper, isReactWrapper, isCheerioWrapper} from './utils';
import shallowToJson from './shallow';
import mountToJson from './mount';
import renderToJson from './render';

export default function (wrapper) {
if (wrapper instanceof ShallowWrapper) {
if (isShallowWrapper(wrapper)) {
return shallowToJson(wrapper);
}

if (wrapper instanceof ReactWrapper) {
if (isReactWrapper(wrapper)) {
return mountToJson(wrapper);
}

if(wrapper.cheerio) {
if(isCheerioWrapper(wrapper)) {
return renderToJson(wrapper);
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/serializer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import ShallowWrapper from 'enzyme/build/ShallowWrapper';
import ReactWrapper from 'enzyme/build/ReactWrapper';

import {isEnzymeWrapper} from './build/utils';
import toJson from './';

module.exports = {
test(wrapper) {
return wrapper instanceof ShallowWrapper || wrapper instanceof ReactWrapper || wrapper.cheerio;
return isEnzymeWrapper(wrapper);
},
print(wrapper) {
return toJson(wrapper);
print(wrapper, serializer) {
return serializer(toJson(wrapper));
},
};
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ShallowWrapper from 'enzyme/build/ShallowWrapper';
import ReactWrapper from 'enzyme/build/ReactWrapper';

const SHALLOW_WRAPPER_NAME = ShallowWrapper.name;
const REACT_WRAPPER_NAME = ReactWrapper.name;

export const isShallowWrapper = wrapper => wrapper && wrapper.constructor && wrapper.constructor.name === SHALLOW_WRAPPER_NAME;
export const isReactWrapper = wrapper => wrapper && wrapper.constructor && wrapper.constructor.name === REACT_WRAPPER_NAME;
export const isCheerioWrapper = wrapper => wrapper.cheerio;

export const isEnzymeWrapper = wrapper => isShallowWrapper(wrapper) || isReactWrapper(wrapper) || isCheerioWrapper(wrapper);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/examples.test.js → tests/core/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { Component } from 'react';
import { shallow } from 'enzyme';
import { shallowToJson } from '../src';
import { shallowToJson } from '../../src';

class MyComponent extends Component {
constructor() {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/focused.test.js → tests/core/focused.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react';
import { shallow } from 'enzyme';
import { shallowToJson } from '../src';
import { shallowToJson } from '../../src';

const MyComponent = props => (
<div className={`my-component ${props.className}`}>
Expand Down
14 changes: 7 additions & 7 deletions test/index.test.js → tests/core/index.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-env jest */

jest.mock('../src/shallow');
jest.mock('../src/mount');
jest.mock('../src/render');
jest.mock('../../src/shallow');
jest.mock('../../src/mount');
jest.mock('../../src/render');

import React from 'react';
import { shallow, mount, render } from 'enzyme';

import shallowToJson from '../src/shallow';
import mountToJson from '../src/mount';
import renderToJson from '../src/render';
import shallowToJson from '../../src/shallow';
import mountToJson from '../../src/mount';
import renderToJson from '../../src/render';

import toJson from '../src/index';
import toJson from '../../src';

it('runs shallowToJson when a shallow wrapper is passed', () => {
const shallowed = shallow(<div>test</div>);
Expand Down
2 changes: 1 addition & 1 deletion test/mount.test.js → tests/core/mount.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react';
import { mount } from 'enzyme';
import { mountToJson } from '../src';
import { mountToJson } from '../../src';
import { BasicPure, BasicWithUndefined } from './fixtures/pure-function';
import { BasicClass, ClassWithPure, ClassWithDirectPure, ClassWithDirectComponent } from './fixtures/class';

Expand Down
2 changes: 1 addition & 1 deletion test/render.test.js → tests/core/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react';
import { render } from 'enzyme';
import { renderToJson } from '../src';
import { renderToJson } from '../../src';
import { BasicPure } from './fixtures/pure-function';
import { BasicClass } from './fixtures/class';

Expand Down
2 changes: 1 addition & 1 deletion test/shallow.test.js → tests/core/shallow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React from 'react';
import { shallow } from 'enzyme';
import { shallowToJson } from '../src';
import { shallowToJson } from '../../src';
import { BasicPure, BasicWithUndefined } from './fixtures/pure-function';
import { BasicClass, ClassWithPure, ClassWithNull } from './fixtures/class';

Expand Down
1 change: 1 addition & 0 deletions tests/serializer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
22 changes: 22 additions & 0 deletions tests/serializer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "serializer-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"jest": {
"snapshotSerializers": [
"<rootDir>/../../serializer"
]
},
"devDependencies": {
"enzyme": "file:../../node_modules/enzyme",
"jest": "file:../../node_modules/jest",
"react": "file:../../node_modules/react"
}
}
20 changes: 20 additions & 0 deletions tests/serializer/tests/__snapshots__/examples.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exports[`test renders correctly 1`] = `
<div
className="my-component"
onClick={[Function]}>
<span
className="count">
1
</span>
<strong>
Hello World!
</strong>
</div>
`;

exports[`test renders span after setState 1`] = `
<span
className="count">
42
</span>
`;
21 changes: 21 additions & 0 deletions tests/serializer/tests/__snapshots__/focused.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
exports[`test renders a \`span\` correctly 1`] = `
<span>
<span>
Hello World!
</span>
</span>
`;

exports[`test renders a \`strong\` correctly 1`] = `
<span>
<strong>
Hello World!
</strong>
</span>
`;

exports[`test renders the right title 1`] = `
<h3>
Component Title
</h3>
`;
148 changes: 148 additions & 0 deletions tests/serializer/tests/__snapshots__/mount.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
exports[`test converts a class mount with a class component in it as a direct child 1`] = `
<ClassWithDirectComponent
className="class">
<ClassWithPure
className="nested-pure">
<div
className="class-with-pure nested-pure"
onClick={[Function]}>
<BasicPure
className="nested-pure">
<div
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<span>
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</div>
</BasicPure>
</div>
</ClassWithPure>
</ClassWithDirectComponent>
`;

exports[`test converts a class mount with a pure function in it 1`] = `
<ClassWithPure
className="class">
<div
className="class-with-pure class"
onClick={[Function]}>
<BasicPure
className="nested-pure">
<div
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</div>
</BasicPure>
</div>
</ClassWithPure>
`;

exports[`test converts a class mount with a pure function in it as a direct child 1`] = `
<ClassWithDirectPure
className="class">
<BasicPure
className="nested-pure">
<div
className="basic-pure nested-pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</span>
<span
className="empty" />
</div>
</div>
</BasicPure>
</ClassWithDirectPure>
`;

exports[`test converts basic class mount 1`] = `
<BasicClass
className="class">
<div
className="basic-class class"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</div>
</div>
</BasicClass>
`;

exports[`test converts basic pure mount 1`] = `
<BasicPure
className="pure">
<div
className="basic-pure pure"
onClick={[Function]}>
<div
className="group"
id="group-id">
<span>
<strong>
Hello!
</strong>
</span>
<span
className="empty" />
</div>
</div>
</BasicPure>
`;

exports[`test skips undefined props 1`] = `
<BasicWithUndefined>
<button>
Hello
</button>
</BasicWithUndefined>
`;
Loading

0 comments on commit 309d6ea

Please sign in to comment.