Skip to content

Commit

Permalink
Merge pull request #112 from ral-facilities/edit-manufacturers-#73
Browse files Browse the repository at this point in the history
Edit manufacturers #73
  • Loading branch information
MatteoGuarnaccia5 authored Nov 9, 2023
2 parents 8869952 + 941052f commit aec47ce
Show file tree
Hide file tree
Showing 9 changed files with 1,323 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Manufacturer', () => {
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":""}'
'{"name":"Manufacturer D","address":{"building_number":"1","street_name":"Example Street","town":"","county":"","postcode":"OX1 2AB"},"telephone":""}'
);
});

Expand Down Expand Up @@ -179,4 +179,130 @@ describe('Manufacturer', () => {
);
});
});
it('Edits a manufacturer correctly', () => {
cy.visit('/inventory-management-system/manufacturer');
cy.findByRole('button', {
name: 'Edit Manufacturer A manufacturer',
}).click();
cy.findByLabelText('Name').clear();
cy.findByLabelText('Name').type('test');

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

cy.findByLabelText('Street name').clear();
cy.findByLabelText('Street name').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('Telephone number').clear();
cy.findByLabelText('Telephone number').type('0000000000');

cy.startSnoopingBrowserMockedRequest();

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

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

it('Trying to edit with duplicate name displays error message', () => {
cy.findByRole('button', {
name: 'Edit Manufacturer A manufacturer',
}).click();

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

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

cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains(
'A manufacturer with the same name has been found. Please enter a different name'
);
});
});

it('Trying to edit with invalid url displays error message', () => {
cy.findByRole('button', {
name: 'Edit Manufacturer A manufacturer',
}).click();

cy.findByLabelText('URL').clear();
cy.findByLabelText('URL').type('invalid');

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

cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains('Please enter a valid URL');
});
});

it('Not changing any fields shows error', () => {
cy.findByRole('button', {
name: 'Edit Manufacturer A manufacturer',
}).click();

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

cy.findByRole('dialog')
.should('be.visible')
.within(() => {
cy.contains(
"There have been no changes made. Please change a field's value or press Cancel to exit"
);
});
});

it('Required fields that are cleared are not allowed and show error message', () => {
cy.findByRole('button', {
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.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.');
});
});
});
70 changes: 70 additions & 0 deletions src/api/manufacturer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { settings } from '../settings';
import {
AddManufacturer,
AddManufacturerResponse,
EditManufacturer,
Manufacturer,
} from '../app.types';

Expand Down Expand Up @@ -104,3 +105,72 @@ export const useDeleteManufacturer = (): UseMutationResult<
},
});
};

const fetchManufacturer = async (
id: string | undefined
): Promise<Manufacturer> => {
let apiUrl: string;
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];
}

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

export const useManufacturer = (
id: string | undefined
): UseQueryResult<Manufacturer, AxiosError> => {
return useQuery<Manufacturer, AxiosError>(
['Manufacturer', id],
(params) => {
return fetchManufacturer(id);
},
{
onError: (error) => {
console.log('Got error ' + error.message);
},
enabled: id !== undefined,
}
);
};

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

export const useEditManufacturer = (): UseMutationResult<
Manufacturer,
AxiosError,
EditManufacturer
> => {
const queryClient = useQueryClient();
return useMutation(
(manufacturer: EditManufacturer) => editManufacturer(manufacturer),
{
onError: (error) => {
console.log('Got error ' + error.message);
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['Manufacturers'] });
},
}
);
};
34 changes: 21 additions & 13 deletions src/app.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ export interface CatalogueCategory {
is_leaf: boolean;
catalogue_item_properties?: CatalogueCategoryFormData[];
}

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

export interface AddManufacturer {
name: string;
url?: string;
Expand All @@ -49,17 +40,26 @@ export interface AddManufacturer {
}

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

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

export interface Manufacturer {
id?: string;
name: string;
url: string;
url?: string;
address: Address;
telephone: string;
}
Expand Down Expand Up @@ -127,6 +127,14 @@ interface Address {
county?: string;
postcode: string;
}

interface EditAddress {
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 aec47ce

Please sign in to comment.