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
46 changes: 40 additions & 6 deletions cypress/e2e/manufacturer/manufacturer.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ describe('Manufacturer', () => {
beforeEach(() => {
cy.visit('/inventory-management-system/manufacturer');
});
afterEach(() => {
cy.clearMocks();
});

it('should render in table headers', () => {
cy.visit('/inventory-management-system/manufacturer');
Expand Down Expand Up @@ -45,7 +48,7 @@ describe('Manufacturer', () => {
cy.url().should('include', 'http://example.com');
});

it('adds a manufacturer with all fields', async () => {
it('adds a manufacturer with all fields', () => {
cy.findByRole('button', { name: 'Add Manufacturer' }).click();
cy.findByLabelText('Name *').type('Manufacturer D');
cy.findByLabelText('URL').type('http://test.co.uk');
Expand All @@ -67,12 +70,12 @@ describe('Manufacturer', () => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(JSON.stringify(request.body)).equal(
'{"name":"Manufacturer D","url":"http://test.co.uk", "address": {building_number: "1", "street_name": "Example Street", "town": "Oxford", "county": "Oxfordshire", "postcode": "OX1 2AB",}, "telephone": "07349612203"}'
'{"name":"Manufacturer D","url":"http://test.co.uk","address":{"building_number":"1","street_name":"Example Street","town":"Oxford","county":"Oxfordshire","postcode":"OX1 2AB"},"telephone":"07349612203"}'
);
});
});

it('adds a manufacturer with only mandatory fields', async () => {
it('adds a manufacturer with only mandatory fields', () => {
cy.findByRole('button', { name: 'Add Manufacturer' }).click();
cy.findByLabelText('Name *').type('Manufacturer D');
cy.findByLabelText('Building number *').type('1');
Expand All @@ -94,7 +97,7 @@ describe('Manufacturer', () => {
);
});

it('render error messages if fields are not filled', async () => {
it('render error messages if fields are not filled', () => {
cy.findByTestId('Add Manufacturer').click();
cy.findByRole('button', { name: 'Save' }).click();
cy.findByRole('dialog')
Expand All @@ -118,7 +121,7 @@ describe('Manufacturer', () => {
cy.contains('Please enter a post code or zip code.');
});
});
it('displays error message when duplicate name entered', async () => {
it('displays error message when duplicate name entered', () => {
cy.findByTestId('Add Manufacturer').click();
cy.findByLabelText('Name *').type('Manufacturer A');
cy.findByLabelText('Building number *').type('1');
Expand All @@ -132,7 +135,7 @@ describe('Manufacturer', () => {
cy.contains('A manufacturer with the same name already exists.');
});
});
it('invalid url displays correct error message', async () => {
it('invalid url displays correct error message', () => {
cy.findByTestId('Add Manufacturer').click();

cy.findByLabelText('URL').type('test.co.uk');
Expand All @@ -145,4 +148,35 @@ describe('Manufacturer', () => {
});
});
});

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', () => {
cy.findAllByTestId('DeleteIcon').eq(1).click();

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

cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains(
'The specified manufacturer is a part of a Catalogue Item. Please delete the Catalogue Item first.'
);
});
});
});
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, Manufacturer } 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: Manufacturer;
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'
);
});
});
28 changes: 28 additions & 0 deletions src/api/manufacturer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,31 @@ export const useAddManufacturer = (): UseMutationResult<
}
);
};

const deleteManufacturer = async (session: Manufacturer): Promise<void> => {
let apiUrl: string;
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];
}
return axios
.delete(`${apiUrl}/v1/manufacturers/${session.id}`, {})
.then((response) => response.data);
};

export const useDeleteManufacturer = (): UseMutationResult<
void,
AxiosError,
Manufacturer
> => {
const queryClient = useQueryClient();
return useMutation((session: Manufacturer) => deleteManufacturer(session), {
onError: (error) => {
console.log('Got error ' + error.message);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['Manufacturers'] });
},
});
};
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
Loading
Loading