import React from 'react';
import {
render,
cleanup,
fireEvent,
// @ts-ignore
waitForDomChange,
} from 'react-testing-library';
import expect from 'expect';
import Mutation from './Mutation';
import CoreAdmin from '../CoreAdmin';
import Resource from '../Resource';
import TestContext from './TestContext';
describe('Mutation', () => {
afterEach(cleanup);
it('should render its child', () => {
const { getByTestId } = render(
{() => (
{() => Hello
}
)}
);
expect(getByTestId('test').textContent).toBe('Hello');
});
it('should pass a callback to trigger the mutation', () => {
let callback = null;
render(
{() => (
{mutate => {
callback = mutate;
return Hello
;
}}
)}
);
expect(callback).toBeInstanceOf(Function);
});
it('should dispatch a fetch action when the mutation callback is triggered', () => {
let dispatchSpy;
const myPayload = {};
const { getByText } = render(
{({ store }) => {
dispatchSpy = jest.spyOn(store, 'dispatch');
return (
{mutate => }
);
}}
);
fireEvent.click(getByText('Hello'));
const action = dispatchSpy.mock.calls[0][0];
expect(action.type).toEqual('CUSTOM_FETCH');
expect(action.payload).toEqual(myPayload);
expect(action.meta.fetch).toEqual('mytype');
expect(action.meta.resource).toEqual('myresource');
});
it('should update the loading state when the mutation callback is triggered', () => {
const myPayload = {};
const { getByText } = render(
{() => (
{(mutate, { loading }) => (
)}
)}
);
expect(getByText('Hello').className).toEqual('idle');
fireEvent.click(getByText('Hello'));
expect(getByText('Hello').className).toEqual('loading');
});
it('should update the data state after a success response', async () => {
const dataProvider = jest.fn();
dataProvider.mockImplementationOnce(() =>
Promise.resolve({ data: { foo: 'bar' } })
);
const Foo = () => (
{(mutate, { data }) => (
)}
);
const { getByTestId } = render(
);
const testElement = getByTestId('test');
expect(testElement.textContent).toBe('no data');
fireEvent.click(testElement);
await waitForDomChange({ container: testElement });
expect(testElement.textContent).toEqual('bar');
});
it('should update the error state after an error response', async () => {
const dataProvider = jest.fn();
dataProvider.mockImplementationOnce(() =>
Promise.reject({ message: 'provider error' })
);
const Foo = () => (
{(mutate, { error }) => (
)}
);
const { getByTestId } = render(
);
const testElement = getByTestId('test');
expect(testElement.textContent).toBe('no data');
fireEvent.click(testElement);
await waitForDomChange({ container: testElement });
expect(testElement.textContent).toEqual('provider error');
});
});