Skip to content

Commit

Permalink
fix: deleted naics code products dont show up on the form
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Jun 2, 2021
1 parent 18836eb commit cdceb85
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
35 changes: 20 additions & 15 deletions app/containers/Forms/ProductRowIdField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ export const ProductRowIdFieldComponent: React.FunctionComponent<Props> = (
* Other props are passed as-is to the StringField.
*/
const fieldProps = useMemo(() => {
const productIds = props.naicsCode?.productsByNaicsCode?.edges.map(
({node}) => node.rowId
);
const productNames = props.naicsCode?.productsByNaicsCode?.edges.map(
({node}) => node.productName
);
const edges = props.naicsCode?.productNaicsCodes.edges || [];
const allowedProducts = edges
.map((e) => e.node.productByProductId)
.sort((a, b) => a.productName.localeCompare(b.productName));

const productIds = allowedProducts.map((p) => p.rowId);
const productNames = allowedProducts.map((p) => p.productName);

return {
...props,
schema: {
...props.schema,
enum: productIds || [],
enumNames: productNames || []
enum: productIds,
enumNames: productNames
},
query: undefined
};
Expand All @@ -60,8 +61,8 @@ export const ProductRowIdFieldComponent: React.FunctionComponent<Props> = (
archivedProductNames: props.query.archived.edges.map(
({node}) => node.productName
),
productIdsByNaicsCode: props.naicsCode?.productsByNaicsCode?.edges.map(
({node}) => node.rowId
productIdsByNaicsCode: props.naicsCode?.productNaicsCodes.edges.map(
(e) => e.node.productByProductId.rowId
)
}),
[props]
Expand Down Expand Up @@ -123,14 +124,18 @@ export default createFragmentContainer(ProductRowIdFieldComponent, {
`,
naicsCode: graphql`
fragment ProductRowIdField_naicsCode on NaicsCode {
productsByNaicsCode: productsByProductNaicsCodeNaicsCodeIdAndProductId(
filter: {productState: {equalTo: PUBLISHED}}
orderBy: PRODUCT_NAME_ASC
productNaicsCodes: productNaicsCodesByNaicsCodeId(
filter: {
deletedAt: {isNull: true}
productByProductId: {productState: {equalTo: PUBLISHED}}
}
) {
edges {
node {
rowId
productName
productByProductId {
rowId
productName
}
}
}
}
Expand Down
41 changes: 30 additions & 11 deletions app/tests/unit/containers/Forms/ProductRowIdField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ describe('The ProductionRowIdField Component with published product matched to a
rowId: 2,
productName: 'dontrenderme'
}
},
{
node: {
rowId: 20,
productName: 'aaaa'
}
}
]
},
Expand All @@ -37,12 +43,22 @@ describe('The ProductionRowIdField Component with published product matched to a
};
const naicsCode: ProductRowIdField_naicsCode = {
' $refType': 'ProductRowIdField_naicsCode',
productsByNaicsCode: {
productNaicsCodes: {
edges: [
{
node: {
rowId: 1,
productName: 'foo'
productByProductId: {
rowId: 1,
productName: 'foo'
}
}
},
{
node: {
productByProductId: {
rowId: 20,
productName: 'aaaa'
}
}
}
]
Expand All @@ -61,18 +77,21 @@ describe('The ProductionRowIdField Component with published product matched to a
expect(wrapper).toMatchSnapshot();
});

it('should contain the products in productsByNaicsCode when passed a naicsCode', () => {
it('should contain the alphabetically sorted products in productsByNaicsCode when passed a naicsCode', () => {
const wrapper = shallow(<ProductRowIdFieldComponent {...props} />);
expect(wrapper.find('StringField').prop('schema' as any).enumNames[0]).toBe(
'foo'
);
expect(
wrapper.find('StringField').prop('schema' as any).enumNames
).toEqual(['aaaa', 'foo']);
});

it('should not contain the products not in productsByNaicsCode when passed a naicsCode', () => {
const wrapper = shallow(<ProductRowIdFieldComponent {...props} />);
expect(wrapper.find('StringField').prop('schema' as any).enumNames[1]).toBe(
undefined
);
expect(
wrapper
.find('StringField')
.prop('schema' as any)
.enumNames.some((name) => name === 'dontrenderme')
).toBe(false);
});
});

Expand All @@ -95,7 +114,7 @@ describe('The ProductionRowIdField Component with archived product', () => {
};
const naicsCode: ProductRowIdField_naicsCode = {
' $refType': 'ProductRowIdField_naicsCode',
productsByNaicsCode: {
productNaicsCodes: {
edges: []
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,22 @@ exports[`The ProductionRowIdField Component with published product matched to a
naicsCode={
Object {
" $refType": "ProductRowIdField_naicsCode",
"productsByNaicsCode": Object {
"productNaicsCodes": Object {
"edges": Array [
Object {
"node": Object {
"productName": "foo",
"rowId": 1,
"productByProductId": Object {
"productName": "foo",
"rowId": 1,
},
},
},
Object {
"node": Object {
"productByProductId": Object {
"productName": "aaaa",
"rowId": 20,
},
},
},
],
Expand Down Expand Up @@ -101,9 +111,11 @@ exports[`The ProductionRowIdField Component with published product matched to a
schema={
Object {
"enum": Array [
20,
1,
],
"enumNames": Array [
"aaaa",
"foo",
],
}
Expand Down

0 comments on commit cdceb85

Please sign in to comment.