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

Change address fields #117 #118

Merged
merged 19 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
69 changes: 31 additions & 38 deletions cypress/e2e/manufacturer/manufacturer.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@ describe('Manufacturer', () => {
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('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'
);
cy.findByText('07334893348').should('be.visible');
cy.findByText('07294958549').should('be.visible');
cy.findByText('07934303412').should('be.visible');
});

it('manufacturer url is correct and opens new webpage', () => {
Expand All @@ -52,8 +46,8 @@ describe('Manufacturer', () => {
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('Country *').type('United Kingdom');
cy.findByLabelText('Address Line *').type('4 Example Street');
cy.findByLabelText('Town').type('Oxford');
cy.findByLabelText('County').type('Oxfordshire');
cy.findByLabelText('Post/Zip code *').type('OX1 2AB');
Expand All @@ -70,16 +64,16 @@ 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":{"address_line":"4 Example Street","town":"Oxford","county":"Oxfordshire","postcode":"OX1 2AB","country":"United Kingdom"},"telephone":"07349612203"}'
);
});
});

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');
cy.findByLabelText('Street name *').type('Example Street');
cy.findByLabelText('Country *').type('United Kingdom');
cy.findByLabelText('Address Line *').type('4 Example Street');
cy.findByLabelText('Post/Zip code *').type('OX1 2AB');

cy.startSnoopingBrowserMockedRequest();
Expand All @@ -93,7 +87,7 @@ describe('Manufacturer', () => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(JSON.stringify(request.body)).equal(
'{"name":"Manufacturer D","address":{"building_number":"1","street_name":"Example Street","town":"","county":"","postcode":"OX1 2AB"},"telephone":""}'
'{"name":"Manufacturer D","address":{"address_line":"4 Example Street","town":null,"county":null,"postcode":"OX1 2AB","country":"United Kingdom"},"telephone":null}'
);
});

Expand All @@ -108,12 +102,12 @@ describe('Manufacturer', () => {
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a building number.');
cy.contains('Please enter a country.');
});
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a street name.');
cy.contains('Please enter an address.');
});
cy.findByRole('dialog')
.should('be.visible')
Expand All @@ -122,11 +116,10 @@ describe('Manufacturer', () => {
});
});
it('displays error message when duplicate name entered', () => {
cy.findByTestId('Add Manufacturer').click();
cy.findByRole('button', { name: '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.findByLabelText('Country *').type('United Kingdom');
cy.findByLabelText('Address Line *').type('4 Example Street');

cy.findByRole('button', { name: 'Save' }).click();
cy.findByRole('dialog')
Expand Down Expand Up @@ -184,23 +177,23 @@ describe('Manufacturer', () => {
cy.findByRole('button', {
name: 'Edit Manufacturer A manufacturer',
}).click();
cy.findByLabelText('Name').clear();
cy.findByLabelText('Name').type('test');
cy.findByLabelText('Name *').clear();
cy.findByLabelText('Name *').type('test');

cy.findByLabelText('Building number').clear();
cy.findByLabelText('Building number').type('100');
cy.findByLabelText('Country *').clear();
cy.findByLabelText('Country *').type('test');

cy.findByLabelText('Street name').clear();
cy.findByLabelText('Street name').type('test');
cy.findByLabelText('Address Line *').clear();
cy.findByLabelText('Address Line *').type('test');

cy.findByLabelText('Town').clear();
cy.findByLabelText('Town').type('test');

cy.findByLabelText('County').clear();
cy.findByLabelText('County').type('test');

cy.findByLabelText('Post/Zip code').clear();
cy.findByLabelText('Post/Zip code').type('test');
cy.findByLabelText('Post/Zip code *').clear();
cy.findByLabelText('Post/Zip code *').type('test');

cy.findByLabelText('Telephone number').clear();
cy.findByLabelText('Telephone number').type('0000000000');
Expand All @@ -216,7 +209,7 @@ describe('Manufacturer', () => {
expect(patchRequests.length).equal(1);
const request = patchRequests[0];
expect(JSON.stringify(request.body)).equal(
'{"name":"test","address":{"building_number":"100","street_name":"test","town":"test","county":"test","postcode":"test"},"telephone":"0000000000"}'
'{"name":"test","address":{"address_line":"test","town":"test","county":"test","postcode":"test","country":"test"},"telephone":"0000000000"}'
);
});
});
Expand All @@ -226,8 +219,8 @@ describe('Manufacturer', () => {
name: 'Edit Manufacturer A manufacturer',
}).click();

cy.findByLabelText('Name').clear();
cy.findByLabelText('Name').type('test_dup');
cy.findByLabelText('Name *').clear();
cy.findByLabelText('Name *').type('test_dup');

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

Expand Down Expand Up @@ -278,10 +271,10 @@ describe('Manufacturer', () => {
name: 'Edit Manufacturer A manufacturer',
}).click();

cy.findByLabelText('Name').clear();
cy.findByLabelText('Building number').clear();
cy.findByLabelText('Street name').clear();
cy.findByLabelText('Post/Zip code').clear();
cy.findByLabelText('Name *').clear();
cy.findByLabelText('Country *').clear();
cy.findByLabelText('Address Line *').clear();
cy.findByLabelText('Post/Zip code *').clear();

cy.findByRole('button', { name: 'Save' }).click();
cy.findByRole('dialog')
Expand All @@ -292,12 +285,12 @@ describe('Manufacturer', () => {
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a building number.');
cy.contains('Please enter a country.');
});
cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a street name.');
cy.contains('Please enter an address.');
});
cy.findByRole('dialog')
.should('be.visible')
Expand Down
30 changes: 15 additions & 15 deletions src/api/manufacturer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { renderHook, waitFor } from '@testing-library/react';
import { AddManufacturer, Manufacturer } from '../app.types';
import { Manufacturer } from '../app.types';
import { hooksWrapperWithProviders } from '../setupTests';
import {
useAddManufacturer,
Expand All @@ -13,17 +13,17 @@ describe('manufacturer api functions', () => {
});

describe('useAddManufacturer', () => {
let mockDataAdd: AddManufacturer;
let mockDataAdd: Manufacturer;
beforeEach(() => {
mockDataAdd = {
name: 'Manufacturer D',
url: 'http://test.co.uk',
address: {
building_number: '1',
street_name: 'Example',
address_line: '4 Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postcode: 'OX1 2AB',
country: 'United Kingdom',
},
telephone: '07349612203',
};
Expand All @@ -43,11 +43,11 @@ describe('manufacturer api functions', () => {
code: 'manufacturer-d',
url: 'http://test.co.uk',
address: {
building_number: '1',
street_name: 'Example Street',
address_line: '4 Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postcode: 'OX1 2AB',
country: 'United Kingdom',
},
telephone: '07349612203',
id: '4',
Expand All @@ -63,17 +63,17 @@ describe('manufacturer api functions', () => {
let mockDataView: Manufacturer;
beforeEach(() => {
mockDataView = {
id: '1',
name: 'Manufacturer A',
url: 'http://example.com',
address: {
building_number: '1',
street_name: 'Example',
address_line: '1 Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postcode: 'OX1 2AB',
country: 'United Kingdom',
},
telephone: '07334893348',
id: '1',
};
});
it('posts a request to delete a manufacturer and return a successful response', async () => {
Expand Down Expand Up @@ -110,11 +110,11 @@ describe('manufacturer api functions', () => {
code: 'manufacturer-a',
url: 'http://example.com',
address: {
building_number: '1',
street_name: 'Example Street',
address_line: '1 Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postcode: 'OX1 2AB',
country: 'United Kingdom',
},
telephone: '07334893348',
},
Expand All @@ -124,11 +124,11 @@ describe('manufacturer api functions', () => {
code: 'manufacturer-b',
url: 'http://test.com',
address: {
building_number: '2',
street_name: 'Example Street',
address_line: '2 Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postcode: 'OX1 2AB',
country: 'United Kingdom',
},
telephone: '07294958549',
},
Expand All @@ -138,11 +138,11 @@ describe('manufacturer api functions', () => {
code: 'manufacturer-c',
url: 'http://test.co.uk',
address: {
building_number: '3',
street_name: 'Example Street',
address_line: '3 Example Street',
town: 'Oxford',
county: 'Oxfordshire',
postcode: 'OX1 2AB',
country: 'United Kingdom',
},
telephone: '07934303412',
},
Expand Down
9 changes: 4 additions & 5 deletions src/api/manufacturer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {
import { settings } from '../settings';

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

const getAllManufacturers = async (): Promise<Manufacturer[]> => {
Expand Down Expand Up @@ -46,7 +45,7 @@ export const useManufacturers = (): UseQueryResult<
};

const addManufacturer = async (
manufacturer: AddManufacturer
manufacturer: Manufacturer
): Promise<AddManufacturerResponse> => {
let apiUrl: string;
apiUrl = '';
Expand All @@ -62,11 +61,11 @@ const addManufacturer = async (
export const useAddManufacturer = (): UseMutationResult<
AddManufacturerResponse,
AxiosError,
AddManufacturer
Manufacturer
> => {
const queryClient = useQueryClient();
return useMutation(
(manufacturer: AddManufacturer) => addManufacturer(manufacturer),
(manufacturer: Manufacturer) => addManufacturer(manufacturer),
{
onError: (error) => {
console.log('Got error ' + error.message);
Expand Down
37 changes: 19 additions & 18 deletions src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,30 @@ export interface CatalogueCategory {
is_leaf: boolean;
catalogue_item_properties?: CatalogueCategoryFormData[];
}
export interface AddManufacturer {
name: string;
url?: string;
address: Address | undefined;
telephone?: string;
}
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved

export interface AddManufacturerResponse {
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
id: string;
name: string;
url: string;
url?: string;
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
address: Address;
telephone: string;
telephone?: string;
code: string;
}

export interface EditManufacturer {
name?: string;
url?: string;
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
address?: EditAddress;
telephone?: string;
id?: string;
telephone?: string | null;
id?: string | null;
}

export interface Manufacturer {
id?: string;
name: string;
url?: string;
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
address: Address;
telephone: string;
address: AddAddress;
telephone: string | null;
}

export interface CatalogueCategoryFormData {
Expand Down Expand Up @@ -121,19 +115,26 @@ export interface ErrorParsing {
}

interface Address {
building_number: string;
street_name: string;
address_line: string;
town?: string;
county?: string;
postcode: string;
country: string;
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
}

interface AddAddress {
address_line: string;
town: string | null;
county: string | null;
postcode: string;
country: string;
}
interface EditAddress {
building_number?: string;
street_name?: string;
town?: string;
county?: string;
address_line?: string;
town?: string | null;
county?: string | null;
postcode?: string;
country?: string;
}
export interface CatalogueCategoryTransferState {
name: string;
Expand Down
Loading
Loading