Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
test: moving oas-form unit tests over to using Jest (#1178)
Browse files Browse the repository at this point in the history
* test: moving oas-form tests over to jest

* chore: removing mocha, sinon, nyc, and chai

* chore: removing unused deps

* fix: removing another instance of cross-env
  • Loading branch information
erunion authored Feb 10, 2021
1 parent cc4daaf commit 4b86ada
Show file tree
Hide file tree
Showing 32 changed files with 11,986 additions and 4,788 deletions.
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ module.exports = {
'\\.svg$': path.join(__dirname, '/lib/svgr-mock.js'),
},
setupFiles: [path.join(__dirname, '/lib/enzyme')],
testMatch: ['**/__tests__/**/(*.)+test.[jt]s?(x)'],
testMatch: [
// Matches:
// - __tests__/*/test.test.js
// - __tests__/*/test_test.js
'**/__tests__/**/(*[._])+test.[jt]s?(x)',
],
testURL: 'http://localhost',
transform: {
'^.+\\.jsx?$': path.join(__dirname, '/lib/babel-jest'),
Expand Down
82 changes: 76 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 1 addition & 14 deletions packages/oas-form/__tests__/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
{
"env": {
"mocha": true
},
"globals": {
"d": true
},
"rules": {
"no-unused-vars": [
2,
{
"varsIgnorePattern": "^d$"
}
]
}
"extends": "@readme/eslint-config/testing"
}
72 changes: 38 additions & 34 deletions packages/oas-form/__tests__/ArrayFieldTemplate_test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
/* eslint-disable max-classes-per-file */
import React, { PureComponent } from 'react';
import { expect } from 'chai';
import { Simulate } from 'react-dom/test-utils';
import { createFormComponent, createSandbox } from './test_utils';
import { createFormComponent } from './test_utils';

describe('ArrayFieldTemplate', () => {
let sandbox;

const formData = ['one', 'two', 'three'];

beforeEach(() => {
sandbox = createSandbox();
});

afterEach(() => {
sandbox.restore();
});

describe('Custom ArrayFieldTemplate of string array', () => {
function ArrayFieldTemplate(props) {
return (
Expand Down Expand Up @@ -54,9 +43,10 @@ describe('ArrayFieldTemplate', () => {
ArrayFieldTemplate,
});

expect(node.querySelectorAll('.field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.field-array .field-content div')).toHaveLength(3);
});
});

describe('with template configured in ui:ArrayFieldTemplate', () => {
it('should render a stateful custom component', () => {
const { node } = createFormComponent({
Expand All @@ -67,9 +57,10 @@ describe('ArrayFieldTemplate', () => {
},
});

expect(node.querySelectorAll('.field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.field-array .field-content div')).toHaveLength(3);
});
});

describe('with template configured globally being overriden in ui:ArrayFieldTemplate', () => {
it('should render a stateful custom component', () => {
const { node } = createFormComponent({
Expand All @@ -82,7 +73,7 @@ describe('ArrayFieldTemplate', () => {
ArrayFieldTemplate: () => <div />,
});

expect(node.querySelectorAll('.field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.field-array .field-content div')).toHaveLength(3);
});
});
});
Expand Down Expand Up @@ -113,6 +104,7 @@ describe('ArrayFieldTemplate', () => {

sharedIts();
});

describe('with template configured in ui:ArrayFieldTemplate', () => {
const uiSchema = {
classNames: 'custom-array',
Expand All @@ -126,8 +118,10 @@ describe('ArrayFieldTemplate', () => {
uiSchema,
}).node;
});

sharedIts();
});

describe('with template configured globally being overriden in ui:ArrayFieldTemplate', () => {
const uiSchema = {
classNames: 'custom-array',
Expand All @@ -143,25 +137,25 @@ describe('ArrayFieldTemplate', () => {
ArrayFieldTemplate: () => <div />,
}).node;
});

sharedIts();
});

function sharedIts() {
it('should render one root element for the array', () => {
expect(node.querySelectorAll('.custom-array')).to.have.length.of(1);
expect(node.querySelectorAll('.custom-array')).toHaveLength(1);
});

it('should render one add button', () => {
expect(node.querySelectorAll('.custom-array-add')).to.have.length.of(1);
expect(node.querySelectorAll('.custom-array-add')).toHaveLength(1);
});

it('should render one child for each array item', () => {
expect(node.querySelectorAll('.custom-array-item')).to.have.length.of(formData.length);
expect(node.querySelectorAll('.custom-array-item')).toHaveLength(formData.length);
});

it('should render text input for each array item', () => {
expect(node.querySelectorAll('.custom-array-item .field input[type=text]')).to.have.length.of(
formData.length
);
expect(node.querySelectorAll('.custom-array-item .field input[type=text]')).toHaveLength(formData.length);
});
}
});
Expand All @@ -180,6 +174,7 @@ describe('ArrayFieldTemplate', () => {
const uiSchema = {
classNames: 'custom-array',
};

beforeEach(() => {
node = createFormComponent({
formData,
Expand All @@ -188,6 +183,7 @@ describe('ArrayFieldTemplate', () => {
ArrayFieldTemplate,
}).node;
});

sharedIts();
});

Expand All @@ -196,20 +192,24 @@ describe('ArrayFieldTemplate', () => {
classNames: 'custom-array',
'ui:ArrayFieldTemplate': ArrayFieldTemplate,
};

beforeEach(() => {
node = createFormComponent({
formData,
schema,
uiSchema,
}).node;
});

sharedIts();
});

describe('with template configured globally being overriden in ui:ArrayFieldTemplate', () => {
const uiSchema = {
classNames: 'custom-array',
'ui:ArrayFieldTemplate': ArrayFieldTemplate,
};

beforeEach(() => {
node = createFormComponent({
formData,
Expand All @@ -219,25 +219,25 @@ describe('ArrayFieldTemplate', () => {
ArrayFieldTemplate: () => <div />,
}).node;
});

sharedIts();
});

function sharedIts() {
it('should render one root element for the array', () => {
expect(node.querySelectorAll('.custom-array')).to.have.length.of(1);
expect(node.querySelectorAll('.custom-array')).toHaveLength(1);
});

it('should not render an add button', () => {
expect(node.querySelectorAll('.custom-array-add')).to.have.length.of(0);
expect(node.querySelectorAll('.custom-array-add')).toHaveLength(0);
});

it('should render one child for each array item', () => {
expect(node.querySelectorAll('.custom-array-item')).to.have.length.of(formData.length);
expect(node.querySelectorAll('.custom-array-item')).toHaveLength(formData.length);
});

it('should render text input for each array item', () => {
expect(node.querySelectorAll('.custom-array-item .field input[type=text]')).to.have.length.of(
formData.length
);
expect(node.querySelectorAll('.custom-array-item .field input[type=text]')).toHaveLength(formData.length);
});
}
});
Expand All @@ -262,7 +262,7 @@ describe('ArrayFieldTemplate', () => {
formData,
ArrayFieldTemplate,
});
expect(node.querySelectorAll('.field-array .field-content div')).to.have.length.of(3);
expect(node.querySelectorAll('.field-array .field-content div')).toHaveLength(3);
});
});

Expand All @@ -274,11 +274,13 @@ describe('ArrayFieldTemplate', () => {
}
return null;
};
createFormComponent({
schema: { type: 'array', items: { type: 'string' } },
formData,
ArrayFieldTemplate,
});
expect(() => {
createFormComponent({
schema: { type: 'array', items: { type: 'string' } },
formData,
ArrayFieldTemplate,
});
}).not.toThrow('Error');
});

it('should pass formData so it is in sync with items', () => {
Expand All @@ -300,7 +302,9 @@ describe('ArrayFieldTemplate', () => {
formData,
ArrayFieldTemplate,
});
Simulate.click(node.querySelector('.array-item-add'));
expect(() => {
Simulate.click(node.querySelector('.array-item-add'));
}).not.toThrow('Error');
});
});
});
Loading

0 comments on commit 4b86ada

Please sign in to comment.