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

Edit manufacturers #73 #112

Merged
merged 22 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7dd27ed
edit logic
MatteoGuarnaccia5 Oct 30, 2023
cfaff6a
changed file/variable names to just be 'manufacturer' not 'addManufac…
MatteoGuarnaccia5 Oct 30, 2023
952cb9d
improved edit logic (pre-populate fields)
MatteoGuarnaccia5 Oct 31, 2023
47efb97
e2e tests
MatteoGuarnaccia5 Oct 31, 2023
b337148
fixed linting #73
MatteoGuarnaccia5 Oct 31, 2023
d583d31
e2e tests fix attempt 1 #73
MatteoGuarnaccia5 Oct 31, 2023
55548f6
removed it.only in e2e tests
MatteoGuarnaccia5 Oct 31, 2023
2600e2c
e2e fully fixed #73
MatteoGuarnaccia5 Oct 31, 2023
c7c0ab0
unit tests and abstraction of error handling
MatteoGuarnaccia5 Nov 1, 2023
a448ca2
error message if no changes made #73
MatteoGuarnaccia5 Nov 1, 2023
6acbd23
added more unit tests #73
MatteoGuarnaccia5 Nov 1, 2023
5f3071b
added more e2e tests #73
MatteoGuarnaccia5 Nov 1, 2023
1873461
removed it.only in e2e tests
MatteoGuarnaccia5 Nov 1, 2023
10daa8e
add manufacturer changes #68
MatteoGuarnaccia5 Nov 7, 2023
a911462
Merge branch 'delete-manufacturer-#72' into edit-manufacturers-#73
MatteoGuarnaccia5 Nov 8, 2023
47cefc7
fixed e2e tests
MatteoGuarnaccia5 Nov 8, 2023
0a8b886
requested changes #73
MatteoGuarnaccia5 Nov 9, 2023
fbbe918
changed interface types #73
MatteoGuarnaccia5 Nov 9, 2023
ca7e98f
changes url test #73
MatteoGuarnaccia5 Nov 9, 2023
a081eb6
Merge branch 'develop' into edit-manufacturers-#73
MatteoGuarnaccia5 Nov 9, 2023
d97f0b5
changed file name #73
MatteoGuarnaccia5 Nov 9, 2023
941052f
removed it.only
MatteoGuarnaccia5 Nov 9, 2023
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
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 {
AddManufacturer,
AddManufacturerResponse,
EditManufacturer,
Manufacturer,
} from '../app.types';

Expand Down Expand Up @@ -51,7 +52,7 @@
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];

Check warning on line 55 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L55

Added line #L55 was not covered by tests
}
return axios
.post<AddManufacturerResponse>(`${apiUrl}/v1/manufacturers`, manufacturer)
Expand Down Expand Up @@ -82,7 +83,7 @@
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];

Check warning on line 86 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L86

Added line #L86 was not covered by tests
}
return axios
.delete(`${apiUrl}/v1/manufacturers/${session.id}`, {})
Expand All @@ -104,3 +105,72 @@
},
});
};

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

Check warning on line 116 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L116

Added line #L116 was not covered by tests
}

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);

Check warning on line 134 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L133-L134

Added lines #L133 - L134 were not covered by tests
},
enabled: id !== undefined,
}
);
};

const editManufacturer = async (
manufacturer: EditManufacturer
): Promise<Manufacturer> => {
let apiUrl: string;
apiUrl = '';
const settingsResult = await settings;
if (settingsResult) {
apiUrl = settingsResult['apiUrl'];

Check warning on line 148 in src/api/manufacturer.tsx

View check run for this annotation

Codecov / codecov/patch

src/api/manufacturer.tsx#L148

Added line #L148 was not covered by tests
}
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 {
MatteoGuarnaccia5 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading