Skip to content

Commit

Permalink
Merge pull request #118 from ral-facilities/address-field-changed-#117
Browse files Browse the repository at this point in the history
Change address fields #117
  • Loading branch information
joshuadkitenge authored Nov 17, 2023
2 parents 83b17a6 + c53efea commit be26ff7
Show file tree
Hide file tree
Showing 11 changed files with 438 additions and 465 deletions.
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
13 changes: 4 additions & 9 deletions src/api/manufacturer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import {
} from '@tanstack/react-query';
import { settings } from '../settings';

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

const getAllManufacturers = async (): Promise<Manufacturer[]> => {
let apiUrl: string;
Expand Down Expand Up @@ -47,20 +42,20 @@ export const useManufacturers = (): UseQueryResult<

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

export const useAddManufacturer = (): UseMutationResult<
AddManufacturerResponse,
Manufacturer,
AxiosError,
AddManufacturer
> => {
Expand Down
54 changes: 24 additions & 30 deletions src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,31 @@ export interface CatalogueCategory {
is_leaf: boolean;
catalogue_item_properties?: CatalogueCategoryFormData[];
}

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

export interface AddManufacturerResponse {
id: string;
name: string;
url: string;
address: Address;
telephone: string;
code: string;
url?: string | null;
address: AddAddress;
telephone?: string | null;
}

export interface EditManufacturer {
name?: string;
url?: string;
url?: string | null;
address?: EditAddress;
telephone?: string;
id?: string;
telephone?: string | null;
id?: string | null;
}

export interface Manufacturer {
id?: string;
export interface ManufacturerDetails {
name: string;
url?: string;
address: Address;
telephone: string;
url?: string | null;
address: AddAddress;
telephone: string | null;
}

export interface Manufacturer extends ManufacturerDetails {
id: string;
}

export interface CatalogueCategoryFormData {
Expand Down Expand Up @@ -126,20 +121,19 @@ export interface ErrorParsing {
detail: string;
}

interface Address {
building_number: string;
street_name: string;
town?: string;
county?: string;
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

0 comments on commit be26ff7

Please sign in to comment.