Skip to content

Translate product properties and specs #30

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

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Translations to product query.

## [1.5.0] - 2024-02-27

### Changed
Expand Down
62 changes: 59 additions & 3 deletions src/__tests__/convertSearchDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import { searchOffersMock } from './mock/searchOffers'
import { convertSearchDocument } from '../convertSearchDocument'
import { searchDocumentMock } from './mock/searchDocument'
import { searchProductMock } from './mock/searchProduct'
import { translationsMock } from './mock/documentTranslations'

let searchDocument: SkuDocument
let searchOffers: SkuOffers
let convertedVtexProduct: SearchProduct
let convertedSearchItems: SearchItem[]
let translations: TranslatedProperty[]

describe('convertSearchDocument', () => {
beforeAll(async () => {
searchDocument = { ...searchDocumentMock }
searchOffers = { ...searchOffersMock }
convertedVtexProduct = (await convertSearchDocument([searchDocument], searchOffers, 'biggy'))[0]
convertedSearchItems = convertedVtexProduct.items
translations = translationsMock
})

it('should convert brand information properly', () => {
Expand Down Expand Up @@ -57,6 +60,7 @@ describe('convertSearchDocument', () => {

expect(linkText).toBe(searchProductMock.linkText)
})

it('should convert productId properly', () => {
const { productId } = convertedVtexProduct

Expand All @@ -82,10 +86,9 @@ describe('convertSearchDocument', () => {
})

it('should convert specifications info properly', () => {
const { specificationGroups, properties } = convertedVtexProduct
const { specificationGroups, properties, skuSpecifications } = convertedVtexProduct

// TODO: Fix skuSpecifications mapping
// expect(skuSpecifications).toMatchObject(searchProductMock.skuSpecifications)
expect(skuSpecifications).toMatchObject(searchProductMock.skuSpecifications)

searchProductMock.specificationGroups.forEach((specification) => {
expect(specificationGroups).toEqual(
Expand All @@ -111,4 +114,57 @@ describe('convertSearchDocument', () => {
expect(item.itemId).toBe(searchProductMock.items[idx].itemId)
})
})

it('should translate product details properly', async () => {
const [translatedProduct] = await convertSearchDocument([searchDocument], searchOffers, 'biggy', translations)
const productName = translations.find((item) => item.field === 'ProductName')?.translation
const description = translations.find((item) => item.field === 'Description')?.translation
const brand = translations.find((item) => item.field === 'BrandName')?.translation

expect(translatedProduct.productName).toEqual(productName)
expect(translatedProduct.description).toEqual(description)
expect(translatedProduct.brand).toEqual(brand)
})

it('should translate product specifications properly', async () => {
const [translatedProduct] = await convertSearchDocument([searchDocument], searchOffers, 'biggy', translations)
const skuSpecificationName = translations.find(
(item) => item.field === 'SpecificationName' && item.context === '30'
)?.translation

const skuSpecificationValue = translations.find(
(item) => item.field === 'SpecificationValue' && item.context === 'Roxo/Lilas'
)?.translation

expect(translatedProduct.skuSpecifications[0].field.name).toEqual(skuSpecificationName)
expect(translatedProduct.skuSpecifications[0].values[0].name).toEqual(skuSpecificationValue)

expect(translatedProduct.specificationGroups[0].name).toEqual(searchProductMock.specificationGroups[0].name)
expect(translatedProduct.specificationGroups[0].specifications[0].name).toEqual(skuSpecificationName)
expect(translatedProduct.specificationGroups[0].specifications[0].values).toEqual([skuSpecificationValue])
})

it('should translate item details properly', async () => {
const [translatedItem] = (
await convertSearchDocument([searchDocument], searchOffers, 'biggy', translations)
)[0].items

const itemName = translations.find((item) => item.field === 'SkuName' && item.context === '326782298')?.translation
const nameComplete = `${translations.find((item) => item.field === 'ProductName')?.translation} ${
translations.find((item) => item.field === 'SkuName' && item.context === '326782298')?.translation
}`

expect(translatedItem.name).toEqual(itemName)
expect(translatedItem.nameComplete).toEqual(nameComplete)
})

it('should translate category names properly', async () => {
const [translatedProduct] = await convertSearchDocument([searchDocument], searchOffers, 'biggy', translations)

const category1 = translations.find((item) => item.field === 'CategoryName' && item.context === '14')?.translation
const category2 = translations.find((item) => item.field === 'CategoryName' && item.context === '356')?.translation

expect(translatedProduct.categories[1].split('/')[1]).toEqual(category1)
expect(translatedProduct.categories[0].split('/')[2]).toEqual(category2)
})
})
57 changes: 57 additions & 0 deletions src/__tests__/mock/documentTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export const translationsMock: TranslatedProperty[] = [
{
field: 'ProductName',
context: '319901251',
translation: 'Apple Iphone 11 128gb 6.1" 12mp Purple',
},
{
field: 'CategoryName',
context: '14',
translation: 'Phones, Smartphones and Smartwatches',
},
{
field: 'CategoryName',
context: '356',
translation: 'Phones',
},
{
field: 'Description',
context: '319901251',
translation: `READ WITH ATTENTION:
What is a Showcase Device:
Showcase device coming directly from the factory, where parts may be exchanged due to launch time and store display, such as:
Screen, Battery, Housing, Camera, Power Connector, etc...
Direct Warranty with Us of 3 Months for Manufacturing Defects and 7 Days for Return and Exchange
We do not guarantee Water Resistance on the Device due to its launch time and the device has already had parts replaced.
Battery above 85% percent
No Original Box
Working FaceID and Clean Icloud
Original device tested and working
Box Contents:
Showcase Apparatus
Lightning cable
Source Toast
Gift Case (According to Stock)
Toast Film`,
},
{
field: 'BrandName',
context: '2000005',
translation: 'Apple',
},
{
field: 'SpecificationName',
context: '30',
translation: 'Variant_Color',
},
{
field: 'SpecificationValue',
context: 'Roxo/Lilas',
translation: 'Purple',
},
{
field: 'SkuName',
context: '326782298',
translation: 'Apple Iphone 11 128gb 6.1" 12mp Purple',
},
]
2 changes: 1 addition & 1 deletion src/__tests__/mock/searchDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export const searchDocumentMock: SkuDocument = {
CategoryId: null,
IsFilter: false,
Position: 1,
IsStockKeppingUnit: false,
IsStockKeppingUnit: true,
IsActive: true,
IsSideMenuLinkActive: false,
IsTopMenuLinkActive: null,
Expand Down
15 changes: 14 additions & 1 deletion src/__tests__/mock/searchProduct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,20 @@ export const searchProductMock: SearchProduct & {
],
},
],
skuSpecifications: [],
skuSpecifications: [
{
field: {
name: 'Variante_Cor',
originalName: 'Variante_Cor',
},
values: [
{
name: 'Roxo/Lilas',
originalName: 'Roxo/Lilas',
},
],
},
],
productClusters: [
{
id: '23022',
Expand Down
2 changes: 1 addition & 1 deletion src/convertSKU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const convertSKU = (product: BiggySearchProduct, indexingType?: IndexingType, tr
videos: sku.videos ?? [],
attachments: [],
isKit: false,
attributes
attributes,
}

variations.forEach((variation) => {
Expand Down
Loading