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

Delete manufacturer #72 #97

Merged
merged 11 commits into from
Nov 8, 2023
33 changes: 33 additions & 0 deletions cypress/e2e/manufacturer/manufacturer.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('Manufacturer', () => {
cy.contains('Please enter a post code or zip code.');
});
});

it('displays error message when duplicate name entered', async () => {
cy.findByTestId('Add Manufacturer').click();
cy.findByLabelText('Name *').type('Manufacturer A');
Expand All @@ -110,6 +111,7 @@ describe('Manufacturer', () => {
cy.contains('A manufacturer with the same name already exists.');
});
});

it('invalid url displays correct error message', async () => {
cy.findByTestId('Add Manufacturer').click();

Expand All @@ -122,4 +124,35 @@ describe('Manufacturer', () => {
cy.contains('Please enter a valid url.');
});
});

it('delete a manufacturer', () => {
cy.findAllByTestId('DeleteIcon').first().click();

cy.startSnoopingBrowserMockedRequest();

cy.findByRole('button', { name: 'Continue' }).click();

cy.findBrowserMockedRequests({
method: 'DELETE',
url: '/v1/manufacturers/:id',
}).should((patchRequests) => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(request.url.toString()).to.contain('1');
});
});

it('shows error when trying to delete manufacturer that is part of Catalogue Item', async () => {
cy.findAllByTestId('DeleteIcon').eq(1).click();

cy.findByRole('button', { name: 'Continue' }).click();

cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains(
'The manufacturer is a part of a Catalogue Item, Please delete the Catalogue Item first Please delete the Catalogue Item first'
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
);
});
});
});
156 changes: 156 additions & 0 deletions src/api/manufacturer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { renderHook, waitFor } from '@testing-library/react';
import { AddManufacturer, ViewManufacturerResponse } from '../app.types';
import { hooksWrapperWithProviders } from '../setupTests';
import {
useAddManufacturer,
useDeleteManufacturer,
useManufacturers,
} from './manufacturer';

describe('manufacturer api functions', () => {
afterEach(() => {
jest.clearAllMocks();
});

describe('useAddManufacturer', () => {
let mockDataAdd: AddManufacturer;
beforeEach(() => {
mockDataAdd = {
name: 'Manufacturer D',
url: 'http://test.co.uk',
address: {
building_number: '1',
street_name: 'Example',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '07349612203',
};
});

it('posts a request to add manufacturer and returns successful response', async () => {
const { result } = renderHook(() => useAddManufacturer(), {
wrapper: hooksWrapperWithProviders(),
});
expect(result.current.isIdle).toBe(true);
result.current.mutate(mockDataAdd);
await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});
expect(result.current.data).toEqual({
name: 'Manufacturer D',
code: 'manufacturer-d',
url: 'http://test.co.uk',
address: {
building_number: '1',
street_name: 'Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '07349612203',
id: '4',
});
});

it.todo(
'sends axios request to fetch records and throws an appropiate error on failure'
);
});

describe('useDeleteManufacturer', () => {
let mockDataView: ViewManufacturerResponse;
beforeEach(() => {
mockDataView = {
name: 'Manufacturer A',
url: 'http://example.com',
address: {
building_number: '1',
street_name: 'Example',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '07334893348',
id: '1',
};
});
it('posts a request to delete a manufacturer and return a successful response', async () => {
const { result } = renderHook(() => useDeleteManufacturer(), {
wrapper: hooksWrapperWithProviders(),
});
expect(result.current.isIdle).toBe(true);
result.current.mutate(mockDataView);
await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});
expect(result.current.data).toEqual('');
});

it.todo(
'sends axios request to fetch records and throws an appropriate error on fetch'
);
});

describe('useManufacturer', () => {
it('sends request to fetch manufacturer data and returns successful response', async () => {
const { result } = renderHook(() => useManufacturers(), {
wrapper: hooksWrapperWithProviders(),
});

await waitFor(() => {
expect(result.current.isSuccess).toBeTruthy();
});

expect(result.current.data).toEqual([
{
id: '1',
name: 'Manufacturer A',
code: 'manufacturer-a',
url: 'http://example.com',
address: {
building_number: '1',
street_name: 'Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '07334893348',
},
{
id: '2',
name: 'Manufacturer B',
code: 'manufacturer-b',
url: 'http://test.com',
address: {
building_number: '2',
street_name: 'Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '07294958549',
},
{
id: '3',
name: 'Manufacturer C',
code: 'manufacturer-c',
url: 'http://test.co.uk',
address: {
building_number: '3',
street_name: 'Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '07934303412',
},
]);
});

it.todo(
'sends axios request to fetch records and throws an appropriate error on failure'
);
});
});
29 changes: 29 additions & 0 deletions src/api/manufacturer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];

Check warning on line 54 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L54

Added line #L54 was not covered by tests
}
return axios
.post<AddManufacturerResponse>(`${apiUrl}/v1/manufacturers`, manufacturer)
Expand All @@ -76,3 +76,32 @@
}
);
};

const deleteManufacturer = async (
session: ViewManufacturerResponse
): Promise<void> => {
let apiUrl: string;
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];

Check warning on line 87 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L87

Added line #L87 was not covered by tests
}
return axios
.delete(`${apiUrl}/v1/manufacturers/${session.id}`, {})
.then((response) => response.data);
};

export const useDeleteManufacturer = (): UseMutationResult<
void,
AxiosError,
ViewManufacturerResponse
> => {
return useMutation(
(session: ViewManufacturerResponse) => deleteManufacturer(session),
{
onError: (error) => {
console.log('Got error ' + error.message);
},
}
);
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
};
2 changes: 1 addition & 1 deletion src/manufacturer/AddmanufacturerDialog.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fireEvent, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import AddManufacturerDialog, {
AddManufacturerDialogProps,
} from './manufacturerDialog.component';
} from './addManufacturerDialog.component';
import { renderComponentWithBrowserRouter } from '../setupTests';
import axios from 'axios';

Expand Down
114 changes: 114 additions & 0 deletions src/manufacturer/DeleteManufacturerDialog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import React from 'react';
import { RenderResult, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderComponentWithBrowserRouter } from '../setupTests';
import { DeleteManufacturerProps } from './deleteManufacturerDialog.component';
import DeleteManufacturerDialog from './deleteManufacturerDialog.component';
import { ViewManufacturerResponse } from '../app.types';

describe('Delete Manufacturer Dialog', () => {
const onClose = jest.fn();
let props: DeleteManufacturerProps;
let manufacturer: ViewManufacturerResponse;
let user;
const createView = (): RenderResult => {
return renderComponentWithBrowserRouter(
<DeleteManufacturerDialog {...props} />
);
};
beforeEach(() => {
manufacturer = {
name: 'test',
url: 'http://example.com',
address: {
building_number: '1',
street_name: 'Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postCode: 'OX1 2AB',
},
telephone: '056896598',
id: '1',
};
props = {
open: true,
onClose: onClose,
manufacturer: manufacturer,
};
user = userEvent;
});
afterEach(() => {
jest.clearAllMocks();
});

it('renders dialog correctly', async () => {
createView();
expect(screen.getByText('Delete Manufacturer')).toBeInTheDocument();
expect(screen.getByTestId('delete-manufacturer-name')).toHaveTextContent(
'test'
);
});

it('calls onClose when Close clicked', async () => {
createView();
const closeButton = screen.getByRole('button', { name: 'Cancel' });
await user.click(closeButton);

await waitFor(() => {
expect(onClose).toHaveBeenCalled();
});
});

it('displays warning message when data not loaded', async () => {
props = {
...props,
manufacturer: undefined,
};
createView();
const continueButton = screen.getByRole('button', { name: 'Continue' });
await user.click(continueButton);
const helperTexts = screen.getByText(
'No data provided, Please refresh and try again'
);
expect(helperTexts).toBeInTheDocument();
expect(onClose).not.toHaveBeenCalled();
});

it('calls handleDelete when Continue clicked', async () => {
createView();
const continueButton = screen.getByRole('button', { name: 'Continue' });
user.click(continueButton);

await waitFor(() => {
expect(onClose).toHaveBeenCalled();
});
});

it('displays error message when user tries to delete a manufacturer that is a part of a catalogue item', async () => {
manufacturer.id = '2';
createView();
const continueButton = screen.getByRole('button', { name: 'Continue' });
user.click(continueButton);

await waitFor(() => {
expect(
screen.getByText(
'The manufacturer is a part of a Catalogue Item, Please delete the Catalogue Item first Please delete the Catalogue Item first'
)
).toBeInTheDocument();
});
});

it('displays error message if an unknown error occurs', async () => {
manufacturer.id = '100';
createView();
const continueButton = screen.getByRole('button', { name: 'Continue' });
user.click(continueButton);

await waitFor(() => {
expect(
screen.getByText('Please refresh and try again')
).toBeInTheDocument();
});
});
});
Loading
Loading