Skip to content

Commit

Permalink
Merge pull request #4941 from WiXSL/assert-to-expect
Browse files Browse the repository at this point in the history
Port assert to expect in tests.
  • Loading branch information
fzaninotto authored Jun 15, 2020
2 parents 2e53d2a + d5e58d9 commit 6f33ec6
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 283 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import * as React from 'react';
import assert from 'assert';
import { cleanup, wait } from '@testing-library/react';
import expect from 'expect';

import ReferenceManyFieldController from './ReferenceManyFieldController';
import renderWithRedux from '../../util/renderWithRedux';

describe('<ReferenceManyFieldController />', () => {
it('should set loaded to false when related records are not yet fetched', () => {
afterEach(cleanup);
it('should set loaded to false when related records are not yet fetched', async () => {
const children = jest.fn().mockReturnValue('children');
const { dispatch } = renderWithRedux(
<ReferenceManyFieldController
Expand Down Expand Up @@ -37,7 +39,8 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);
assert.deepEqual(dispatch.mock.calls[0], [
await wait();
expect(dispatch.mock.calls[0]).toEqual([
{
meta: {
relatedTo: 'foo_bar@foo_id_undefined',
Expand All @@ -55,7 +58,7 @@ describe('<ReferenceManyFieldController />', () => {
]);
});

it('should pass data and ids to children function', () => {
it('should pass data and ids to children function', async () => {
const children = jest.fn().mockReturnValue('children');
const data = {
1: { id: 1, title: 'hello' },
Expand Down Expand Up @@ -95,11 +98,12 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);
assert.deepEqual(children.mock.calls[0][0].data, data);
assert.deepEqual(children.mock.calls[0][0].ids, [1, 2]);
await wait();
expect(children.mock.calls[0][0].data).toEqual(data);
expect(children.mock.calls[0][0].ids).toEqual([1, 2]);
});

it('should support record with string identifier', () => {
it('should support record with string identifier', async () => {
const children = jest.fn().mockReturnValue('children');
renderWithRedux(
<ReferenceManyFieldController
Expand Down Expand Up @@ -135,14 +139,15 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);
assert.deepEqual(children.mock.calls[0][0].data, {
await wait();
expect(children.mock.calls[0][0].data).toEqual({
'abc-1': { id: 'abc-1', title: 'hello' },
'abc-2': { id: 'abc-2', title: 'world' },
});
assert.deepEqual(children.mock.calls[0][0].ids, ['abc-1', 'abc-2']);
expect(children.mock.calls[0][0].ids).toEqual(['abc-1', 'abc-2']);
});

it('should support custom source', () => {
it('should support custom source', async () => {
const children = jest.fn(({ data }) =>
data && data.length > 0 ? data.length : null
);
Expand Down Expand Up @@ -182,8 +187,8 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);

assert.deepEqual(dispatch.mock.calls[0], [
await wait();
expect(dispatch.mock.calls[0]).toEqual([
{
meta: {
relatedTo: 'posts_comments@post_id_1',
Expand All @@ -209,7 +214,7 @@ describe('<ReferenceManyFieldController />', () => {
});
});

it('should call crudGetManyReference when its props changes', () => {
it('should call crudGetManyReference when its props changes', async () => {
const ControllerWrapper = props => (
<ReferenceManyFieldController
record={{ id: 1 }}
Expand All @@ -232,12 +237,11 @@ describe('<ReferenceManyFieldController />', () => {
},
},
});

expect(dispatch).toBeCalledTimes(3); // CRUD_GET_MANY_REFERENCE, CRUD_GET_MANY_REFERENCE_LOADING, FETCH_START
rerender(<ControllerWrapper sort={{ field: 'id', order: 'ASC' }} />);
expect(dispatch).toBeCalledTimes(6);

assert.deepEqual(dispatch.mock.calls[0], [
await wait();
expect(dispatch.mock.calls[0]).toEqual([
{
meta: {
relatedTo: 'foo_bar@foo_id_1',
Expand All @@ -254,7 +258,7 @@ describe('<ReferenceManyFieldController />', () => {
},
]);

assert.deepEqual(dispatch.mock.calls[3], [
expect(dispatch.mock.calls[3]).toEqual([
{
meta: {
relatedTo: 'foo_bar@foo_id_1',
Expand Down
31 changes: 15 additions & 16 deletions packages/ra-core/src/dataProvider/fetch.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
import expect from 'expect';
import {
createHeadersFromOptions,
queryParameters,
Expand All @@ -8,57 +8,56 @@ import {
describe('queryParameters', () => {
it('should generate a query parameter', () => {
const data = { foo: 'bar' };
assert.equal(queryParameters(data), 'foo=bar');
expect(queryParameters(data)).toEqual('foo=bar');
});

it('should generate multiple query parameters', () => {
const data = { foo: 'fooval', bar: 'barval' };
const actual = queryParameters(data);
assert(
['foo=fooval&bar=barval', 'bar=barval&foo=fooval'].includes(actual)
expect(['foo=fooval&bar=barval', 'bar=barval&foo=fooval']).toContain(
actual
);
});

it('should generate multiple query parameters with a same name', () => {
const data = { foo: ['bar', 'baz'] };
assert.equal(queryParameters(data), 'foo=bar&foo=baz');
expect(queryParameters(data)).toEqual('foo=bar&foo=baz');
});

it('should generate an encoded query parameter', () => {
const data = ['foo=bar', 'foo?bar&baz'];
assert.equal(
queryParameters({ [data[0]]: data[1] }),
expect(queryParameters({ [data[0]]: data[1] })).toEqual(
data.map(encodeURIComponent).join('=')
);
});
});

describe('flattenObject', () => {
it('should return null with null', () => {
assert.strictEqual(flattenObject(null), null);
expect(flattenObject(null)).toBeNull();
});

it('should return itself with a string', () => {
assert.equal(flattenObject('foo'), 'foo');
expect(flattenObject('foo')).toEqual('foo');
});

it('should return itself with an array', () => {
assert.deepEqual(flattenObject(['foo']), ['foo']);
expect(flattenObject(['foo'])).toEqual(['foo']);
});

it('should return a same object with an empty object', () => {
assert.deepEqual(flattenObject({}), {});
expect(flattenObject({})).toEqual({});
});

it('should return a same object with a non-nested object', () => {
const value = { foo: 'fooval', bar: 'barval' };
assert.deepEqual(flattenObject(value), value);
expect(flattenObject(value)).toEqual(value);
});

it('should return a same object with a nested object', () => {
const input = { foo: 'foo', bar: { baz: 'barbaz' } };
const expected = { foo: 'foo', 'bar.baz': 'barbaz' };
assert.deepEqual(flattenObject(input), expected);
expect(flattenObject(input)).toEqual(expected);
});
});

Expand All @@ -69,7 +68,7 @@ describe('createHeadersFromOptions', () => {
};

const headers = createHeadersFromOptions(options);
assert.strictEqual(headers.get('Content-Type'), 'application/json');
expect(headers.get('Content-Type')).toStrictEqual('application/json');
});

it('should not add a Content-Type header for GET requests', () => {
Expand All @@ -79,11 +78,11 @@ describe('createHeadersFromOptions', () => {
};

const headersWithMethod = createHeadersFromOptions(optionsWithMethod);
assert.strictEqual(headersWithMethod.get('Content-Type'), null);
expect(headersWithMethod.get('Content-Type')).toBeNull();

const headersWithoutMethod = createHeadersFromOptions(
optionsWithoutMethod
);
assert.strictEqual(headersWithoutMethod.get('Content-Type'), null);
expect(headersWithoutMethod.get('Content-Type')).toBeNull();
});
});
9 changes: 4 additions & 5 deletions packages/ra-core/src/form/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
import expect from 'expect';

import {
required,
Expand All @@ -15,12 +15,11 @@ import {

describe('Validators', () => {
const test = (validator, inputs, message) =>
assert.deepEqual(
expect(
inputs
.map(input => validator(input, null))
.map(error => (error && error.message ? error.message : error)),
Array(...Array(inputs.length)).map(() => message)
);
.map(error => (error && error.message ? error.message : error))
).toEqual(Array(...Array(inputs.length)).map(() => message));

describe('composeValidators', () => {
it('Correctly composes validators passed as an array', () => {
Expand Down
18 changes: 8 additions & 10 deletions packages/ra-core/src/reducer/admin/notifications.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
import expect from 'expect';
import {
HIDE_NOTIFICATION,
SHOW_NOTIFICATION,
Expand All @@ -8,11 +8,12 @@ import reducer from './notifications';

describe('notifications reducer', () => {
it('should return empty notification by default', () => {
assert.deepEqual([], reducer(undefined, { type: 'foo' }));
expect(reducer(undefined, { type: 'foo' })).toEqual([]);
});
it('should set autoHideDuration when passed in payload', () => {
assert.deepEqual(
[{ message: 'test', type: 'info', autoHideDuration: 1337 }],
expect([
{ message: 'test', type: 'info', autoHideDuration: 1337 },
]).toEqual(
reducer(undefined, {
type: SHOW_NOTIFICATION,
payload: {
Expand All @@ -24,8 +25,7 @@ describe('notifications reducer', () => {
);
});
it('should set text and type upon SHOW_NOTIFICATION', () => {
assert.deepEqual(
[{ message: 'foo', type: 'warning' }],
expect([{ message: 'foo', type: 'warning' }]).toEqual(
reducer(undefined, {
type: SHOW_NOTIFICATION,
payload: {
Expand All @@ -36,8 +36,7 @@ describe('notifications reducer', () => {
);
});
it('should have no elements upon last HIDE_NOTIFICATION', () => {
assert.deepEqual(
[],
expect([]).toEqual(
reducer([{ message: 'foo', type: 'warning' as NotificationType }], {
type: HIDE_NOTIFICATION,
})
Expand All @@ -48,8 +47,7 @@ describe('notifications reducer', () => {
{ message: 'foo', type: 'info' as NotificationType },
{ message: 'bar', type: 'info' as NotificationType },
];
assert.equal(
notifications.length - 1,
expect(notifications.length - 1).toEqual(
reducer(notifications, {
type: HIDE_NOTIFICATION,
}).length
Expand Down
24 changes: 11 additions & 13 deletions packages/ra-core/src/reducer/admin/references/oneToMany.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
import expect from 'expect';

import oneToManyReducer, { nameRelatedTo } from './oneToMany';
import { DELETE, DELETE_MANY } from '../../../core';
Expand All @@ -7,30 +7,28 @@ import { UNDOABLE } from '../../../actions';
describe('oneToMany', () => {
describe('oneToMany', () => {
it('should name relation based on reference, id, resource and target', () => {
assert.equal(
nameRelatedTo('reference', 'id', 'resource', 'target'),
'resource_reference@target_id'
);
assert.equal(
nameRelatedTo('comments', '6', 'posts', 'id'),
expect(
nameRelatedTo('reference', 'id', 'resource', 'target')
).toEqual('resource_reference@target_id');
expect(nameRelatedTo('comments', '6', 'posts', 'id')).toEqual(
'posts_comments@id_6'
);
});

it('should incorporate filter to the name if any', () => {
assert.equal(
expect(
nameRelatedTo('reference', 'id', 'resource', 'target', {
filter1: 'value1',
filter2: false,
}),
})
).toEqual(
'resource_reference@target_id?filter1="value1"&filter2=false'
);
assert.equal(
expect(
nameRelatedTo('comments', '6', 'posts', 'id', {
active: true,
}),
'posts_comments@id_6?active=true'
);
})
).toEqual('posts_comments@id_6?active=true');
});

it('should remove reference deleted optimistically', () => {
Expand Down
Loading

0 comments on commit 6f33ec6

Please sign in to comment.