-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix map shipping translations to translation entities (#5303)
* Fix map shipping translations to translation entities * Add changeset * Exort sumCompleted
- Loading branch information
Showing
4 changed files
with
157 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
Adding tranalstions to shipping methods no more cause error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { ShippingMethodTranslationsQuery } from "@dashboard/graphql"; | ||
|
||
import { mapTranslationsToEntities } from "./utils"; | ||
|
||
describe("mapTranslationsToEntities", () => { | ||
it("should return empty array if data is undefined", () => { | ||
// Arrange | ||
const data = undefined; | ||
|
||
// Act | ||
const result = mapTranslationsToEntities(data); | ||
|
||
// Assert | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should return empty array if translations is undefined", () => { | ||
// Arrange | ||
const data = { | ||
translations: undefined, | ||
} as unknown as ShippingMethodTranslationsQuery; | ||
|
||
// Act | ||
const result = mapTranslationsToEntities(data); | ||
|
||
// Assert | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should return empty array if items is undefined", () => { | ||
// Arrange | ||
const data = { | ||
translations: { | ||
edges: undefined, | ||
}, | ||
} as unknown as ShippingMethodTranslationsQuery; | ||
|
||
// Act | ||
const result = mapTranslationsToEntities(data); | ||
|
||
// Assert | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
it("should return correct array", () => { | ||
// Arrange | ||
const data = { | ||
translations: { | ||
edges: [ | ||
{ | ||
node: { | ||
__typename: "ShippingMethodTranslatableContent", | ||
translation: { | ||
name: "name", | ||
description: "description", | ||
}, | ||
shippingMethod: { | ||
id: "id", | ||
}, | ||
name: "name", | ||
}, | ||
}, | ||
], | ||
}, | ||
} as ShippingMethodTranslationsQuery; | ||
|
||
// Act | ||
const result = mapTranslationsToEntities(data); | ||
|
||
// Assert | ||
expect(result).toEqual([ | ||
{ | ||
completion: { | ||
current: 2, | ||
max: 2, | ||
}, | ||
id: "id", | ||
name: "name", | ||
}, | ||
]); | ||
}); | ||
|
||
it("should return empty string when no shipping method", () => { | ||
// Arrange | ||
const data = { | ||
translations: { | ||
edges: [ | ||
{ | ||
node: { | ||
__typename: "ShippingMethodTranslatableContent", | ||
translation: { | ||
name: "name", | ||
description: "description", | ||
}, | ||
name: "name", | ||
}, | ||
}, | ||
], | ||
}, | ||
} as ShippingMethodTranslationsQuery; | ||
|
||
// Act | ||
const result = mapTranslationsToEntities(data); | ||
|
||
// Assert | ||
expect(result).toEqual([ | ||
{ | ||
completion: { | ||
current: 2, | ||
max: 2, | ||
}, | ||
id: "", | ||
name: "name", | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,36 @@ | ||
import { ShippingMethodTranslationsQuery } from "@dashboard/graphql"; | ||
import { TranslatableEntity } from "@dashboard/translations/components/TranslationsEntitiesList"; | ||
import { mapEdgesToItems } from "@dashboard/utils/maps"; | ||
|
||
export function mapTranslationsToEntities( | ||
data: ShippingMethodTranslationsQuery | undefined, | ||
): TranslatableEntity[] { | ||
if (!data?.translations) { | ||
return []; | ||
} | ||
|
||
const items = mapEdgesToItems(data.translations); | ||
|
||
if (!items) { | ||
return []; | ||
} | ||
|
||
return items.reduce((acc, node) => { | ||
if (node.__typename === "ShippingMethodTranslatableContent") { | ||
acc.push({ | ||
completion: { | ||
current: sumCompleted([node.translation?.name, node.translation?.description]), | ||
max: 2, | ||
}, | ||
id: node?.shippingMethod?.id ?? "", | ||
name: node?.name, | ||
}); | ||
} | ||
|
||
return acc; | ||
}, [] as TranslatableEntity[]); | ||
} | ||
|
||
export function sumCompleted(list: any[]): number { | ||
return list.reduce((acc, field) => acc + (field ? 1 : 0), 0); | ||
} |