Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests] [Refactor] move and share shallow/mount tests to individual files for each method #2070

Merged
merged 4 commits into from Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -15,7 +15,7 @@ before_script:
script:
- 'if [ -n "${LINT-}" ]; then npm run lint; elif [ -n "${KARMA-}" ]; then npm run test:karma -- --single-run; elif [ -n "${REACT-}" ]; then npm run travis; else false ; fi'
after_script:
- 'if [ -n "${REACT-}" ]; then case "${TRAVIS_NODE_VERSION}" in "10" | "4" | "0.12") cat ./coverage/lcov.info | ./node_modules/.bin/coveralls ;; esac ; fi'
- 'if [ -n "${REACT-}" ]; then case "${TRAVIS_NODE_VERSION}" in "8" | "6") ;; *) cat ./coverage/lcov.info | ./node_modules/.bin/coveralls ;; esac ; fi'
sudo: false
matrix:
fast_finish: true
Expand Down
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -85,6 +85,19 @@ npm run build:watch
npm run test:watch
```

### Tests for functionality shared between `shallow` and `mount`

Tests for a method "foo" are stored in `packages/enzyme-test-suite/test/shared/methods/foo`. The file default exports a function that receives an injected object argument, containing the following properties:
- `Wrap`: e.g. `shallow`, `mount`
- `WrapRendered`: this abstracts around the differences between `shallow` and `mount` - e.g., that the root of a shallow wrapper around `Foo` is what `Foo` *renders*, where the root of a mount wrapper around `Foo` is `Foo` itself. Thus, this function produces a wrapper around what `Foo` renders, regardless of the `Wrap` method used.
- `Wrapper`: e.g. `ShallowWrapper`, `ReactWrapper`
- `WrapperName`: e.g. `"ShallowWrapper"`, `"ReactWrapper"`
- `isShallow`: true if `shallow`. note: needing to use this is a code smell, please avoid.
- `isMount`: true if `mount`. note: needing to use this is a code smell, please avoid.
- `makeDOMElement`: in `mount`, makes a real DOM element; in `shallow`, makes a mock object.

These tests are ran via an explicit list in a `describeMethods` call in the ReactWrapper and ShallowWrapper test files. If you add a new test file for a shared method, you'll need to add its name to both calls.

### Style & Linting

This codebase adheres to the [Airbnb Styleguide](https://github.com/airbnb/javascript) and is
Expand Down
37 changes: 36 additions & 1 deletion packages/enzyme-test-suite/test/Adapter-spec.jsx
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { expect } from 'chai';
import jsdom from 'jsdom';
import { get } from 'enzyme/build/configuration';
import { configure, shallow } from 'enzyme';
import { configure, shallow, EnzymeAdapter } from 'enzyme';
import inspect from 'object-inspect';
import {
Portal,
Expand Down Expand Up @@ -77,6 +77,41 @@ describe('Adapter', () => {
});
});

describe('base class', () => {
it('constructs', () => {
const instance = new EnzymeAdapter();
expect(instance).to.have.property('options');
expect(instance.options).to.be.an('object');
});

it('throws on abstract methods', () => {
expect(() => new EnzymeAdapter().createRenderer()).to.throw(
Error,
'createRenderer is a required method of EnzymeAdapter, but was not implemented.',
);
expect(() => new EnzymeAdapter().nodeToElement()).to.throw(
Error,
'nodeToElement is a required method of EnzymeAdapter, but was not implemented.',
);
expect(() => new EnzymeAdapter().isValidElement()).to.throw(
Error,
'isValidElement is a required method of EnzymeAdapter, but was not implemented.',
);
expect(() => new EnzymeAdapter().createElement()).to.throw(
Error,
'createElement is a required method of EnzymeAdapter, but was not implemented.',
);
});

describe('invokeSetStateCallback', () => {
it('has the right length', () => {
expect(EnzymeAdapter.prototype).to.have.property('invokeSetStateCallback');
expect(EnzymeAdapter.prototype.invokeSetStateCallback).to.be.a('function');
expect(EnzymeAdapter.prototype.invokeSetStateCallback).to.have.lengthOf(2);
});
});
});

describeWithDOM('mounted render', () => {
function hydratedTreeMatchesUnhydrated(element, hydrate = false) {
const markup = renderToString(element);
Expand Down