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

Port assert to expect in tests. #4941

Merged
merged 5 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import assert from 'assert';
import expect from 'expect';

import ReferenceManyFieldController from './ReferenceManyFieldController';
import renderWithRedux from '../../util/renderWithRedux';
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);
assert.deepEqual(dispatch.mock.calls[0], [
expect(dispatch.mock.calls[0]).toEqual([
{
meta: {
relatedTo: 'foo_bar@foo_id_undefined',
Expand Down Expand Up @@ -95,8 +95,8 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);
assert.deepEqual(children.mock.calls[0][0].data, data);
assert.deepEqual(children.mock.calls[0][0].ids, [1, 2]);
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', () => {
Expand Down Expand Up @@ -135,11 +135,11 @@ describe('<ReferenceManyFieldController />', () => {
},
}
);
assert.deepEqual(children.mock.calls[0][0].data, {
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', () => {
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('<ReferenceManyFieldController />', () => {
}
);

assert.deepEqual(dispatch.mock.calls[0], [
expect(dispatch.mock.calls[0]).toEqual([
{
meta: {
relatedTo: 'posts_comments@post_id_1',
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('<ReferenceManyFieldController />', () => {
rerender(<ControllerWrapper sort={{ field: 'id', order: 'ASC' }} />);
expect(dispatch).toBeCalledTimes(6);

assert.deepEqual(dispatch.mock.calls[0], [
expect(dispatch.mock.calls[0]).toEqual([
{
meta: {
relatedTo: 'foo_bar@foo_id_1',
Expand All @@ -254,7 +254,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')).toStrictEqual(null);
WiXSL marked this conversation as resolved.
Show resolved Hide resolved

const headersWithoutMethod = createHeadersFromOptions(
optionsWithoutMethod
);
assert.strictEqual(headersWithoutMethod.get('Content-Type'), null);
expect(headersWithoutMethod.get('Content-Type')).toStrictEqual(null);
});
});
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([]).toEqual(reducer(undefined, { type: 'foo' }));
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
});
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