Skip to content

Commit

Permalink
Merge pull request #80 from ral-facilities/add-manufacturers-#68
Browse files Browse the repository at this point in the history
Add manufacturers #68
  • Loading branch information
MatteoGuarnaccia5 authored Nov 8, 2023
2 parents 9688e29 + 92ae589 commit 1b205cb
Show file tree
Hide file tree
Showing 9 changed files with 1,124 additions and 123 deletions.
116 changes: 112 additions & 4 deletions cypress/e2e/manufacturer/manufacturer.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('Manufacturer', () => {
cy.findByText('Name').should('be.visible');
cy.findByText('URL').should('be.visible');
cy.findByText('Address').should('be.visible');
cy.findByText('Telephone').should('be.visible');
});

it('should render manufacturer data', () => {
Expand All @@ -18,11 +19,17 @@ describe('Manufacturer', () => {
cy.findByText('Manufacturer B').should('be.visible');
cy.findByText('Manufacturer C').should('be.visible');
cy.findByText('http://example.com').should('be.visible');
cy.findByText('http://test.com').should('be.visible');
cy.findByText('http://test.co.uk').should('be.visible');
cy.findByText('http://123test.com').should('be.visible');
cy.findByText('10 My Street').should('be.visible');
cy.findByText('11 My Street').should('be.visible');
cy.findByText('12 My Street').should('be.visible');
cy.findByText('1 Example Street Oxford Oxfordshire OX1 2AB').should(
'be.visible'
);
cy.findByText('2 Example Street Oxford Oxfordshire OX1 2AB').should(
'be.visible'
);
cy.findByText('3 Example Street Oxford Oxfordshire OX1 2AB').should(
'be.visible'
);
});

it('manufacturer url is correct and opens new webpage', () => {
Expand All @@ -37,4 +44,105 @@ describe('Manufacturer', () => {
.click();
cy.url().should('include', 'http://example.com');
});

it('adds a manufacturer with all fields', async () => {
cy.findByRole('button', { name: 'Add Manufacturer' }).click();
cy.findByLabelText('Name *').type('Manufacturer D');
cy.findByLabelText('URL').type('http://test.co.uk');
cy.findByLabelText('Building number *').type('1');
cy.findByLabelText('Street name *').type('Example Street');
cy.findByLabelText('Town').type('Oxford');
cy.findByLabelText('County').type('Oxfordshire');
cy.findByLabelText('Post/Zip code *').type('OX1 2AB');
cy.findByLabelText('Telephone number').type('07349612203');

cy.startSnoopingBrowserMockedRequest();

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

cy.findBrowserMockedRequests({
method: 'POST',
url: '/v1/manufacturers',
}).should((patchRequests) => {
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"}'
);
});
});

it('adds a manufacturer with only mandatory fields', async () => {
cy.findByRole('button', { name: 'Add Manufacturer' }).click();
cy.findByLabelText('Name *').type('Manufacturer D');
cy.findByLabelText('Building number *').type('1');
cy.findByLabelText('Street name *').type('Example Street');
cy.findByLabelText('Post/Zip code *').type('OX1 2AB');

cy.startSnoopingBrowserMockedRequest();

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

cy.findBrowserMockedRequests({
method: 'POST',
url: '/v1/manufacturers',
}).should((patchRequests) => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(JSON.stringify(request.body)).equal(
'{"name":"Manufacturer D","url":"","address":{"building_number":"1","street_name":"Example Street","town":"","county":"","postcode":"OX1 2AB"},"telephone":""}'
);
});

it('render error messages if fields are not filled', async () => {
cy.findByTestId('Add Manufacturer').click();
cy.findByRole('button', { name: 'Save' }).click();
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a name.');
});
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a building number.');
});
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a street name.');
});
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
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');
cy.findByLabelText('Building number *').type('1');
cy.findByLabelText('Street name *').type('Example Street');
cy.findByLabelText('Post/Zip code *').type('OX1 2AB');

cy.findByRole('button', { name: 'Save' }).click();
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('A manufacturer with the same name already exists.');
});
});
it('invalid url displays correct error message', async () => {
cy.findByTestId('Add Manufacturer').click();

cy.findByLabelText('URL').type('test.co.uk');

cy.findByRole('button', { name: 'Save' }).click();
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a valid url.');
});
});
});
});
49 changes: 46 additions & 3 deletions src/api/manufacturer.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import axios, { AxiosError } from 'axios';
import { useQuery, UseQueryResult } from '@tanstack/react-query';
import {
useMutation,
UseMutationResult,
useQuery,
useQueryClient,
UseQueryResult,
} from '@tanstack/react-query';
import { settings } from '../settings';

import { Manufacturer } from '../app.types';
import {
AddManufacturer,
AddManufacturerResponse,
Manufacturer,
} from '../app.types';

const getAllManufacturers = async (): Promise<Manufacturer[]> => {
let apiUrl: string;
Expand All @@ -13,7 +23,7 @@ const getAllManufacturers = async (): Promise<Manufacturer[]> => {
}

return axios
.get(`${apiUrl}/v1/manufacturer`, {})
.get(`${apiUrl}/v1/manufacturers`, {})
.then((response) => response.data);
};

Expand All @@ -33,3 +43,36 @@ export const useManufacturers = (): UseQueryResult<
}
);
};

const addManufacturer = async (
manufacturer: AddManufacturer
): Promise<AddManufacturerResponse> => {
let apiUrl: string;
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];
}
return axios
.post<AddManufacturerResponse>(`${apiUrl}/v1/manufacturers`, manufacturer)
.then((response) => response.data);
};

export const useAddManufacturer = (): UseMutationResult<
AddManufacturerResponse,
AxiosError,
AddManufacturer
> => {
const queryClient = useQueryClient();
return useMutation(
(manufacturer: AddManufacturer) => addManufacturer(manufacturer),
{
onError: (error) => {
console.log('Got error ' + error.message);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['Manufacturers'] });
},
}
);
};
33 changes: 32 additions & 1 deletion src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,34 @@ export interface CatalogueCategory {
export interface Manufacturer {
name: string;
url: string;
address: string;
address: Address;
telephone: string;
id: string;
}

export interface AddManufacturer {
name: string;
url?: string;
address: Address | undefined;
telephone?: string;
}

export interface AddManufacturerResponse {
name: string;
code: string;
url: string;
address: Address;
telephone: string;
id: string;
}

export interface ManufacturerDetail {
name: string;
url: string;
address: Address;
telephone: string;
}

export interface CatalogueCategoryFormData {
name: string;
type: string;
Expand Down Expand Up @@ -96,6 +120,13 @@ export interface ErrorParsing {
detail: string;
}

interface Address {
building_number: string;
street_name: string;
town?: string;
county?: string;
postcode: string;
}
export interface CatalogueCategoryTransferState {
name: string;
message: string;
Expand Down
Loading

0 comments on commit 1b205cb

Please sign in to comment.