From 19d3447e1067f23cb305f3a86af699305765d86a Mon Sep 17 00:00:00 2001 From: Thalyta Fabrine Date: Tue, 27 Feb 2024 10:49:27 +0100 Subject: [PATCH] Add tests --- src/__tests__/convertSearchDocument.test.ts | 62 ++++++++++++++++++++- src/__tests__/mock/documentTranslations.ts | 57 +++++++++++++++++++ src/__tests__/mock/searchDocument.ts | 2 +- src/__tests__/mock/searchProduct.ts | 15 ++++- src/convertSearchDocument.ts | 6 -- src/typings/global.ts | 6 ++ 6 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 src/__tests__/mock/documentTranslations.ts diff --git a/src/__tests__/convertSearchDocument.test.ts b/src/__tests__/convertSearchDocument.test.ts index 91b3cad..6b267f9 100644 --- a/src/__tests__/convertSearchDocument.test.ts +++ b/src/__tests__/convertSearchDocument.test.ts @@ -2,11 +2,13 @@ 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 () => { @@ -14,6 +16,7 @@ describe('convertSearchDocument', () => { searchOffers = { ...searchOffersMock } convertedVtexProduct = (await convertSearchDocument([searchDocument], searchOffers, 'biggy'))[0] convertedSearchItems = convertedVtexProduct.items + translations = translationsMock }) it('should convert brand information properly', () => { @@ -57,6 +60,7 @@ describe('convertSearchDocument', () => { expect(linkText).toBe(searchProductMock.linkText) }) + it('should convert productId properly', () => { const { productId } = convertedVtexProduct @@ -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( @@ -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) + }) }) diff --git a/src/__tests__/mock/documentTranslations.ts b/src/__tests__/mock/documentTranslations.ts new file mode 100644 index 0000000..173e297 --- /dev/null +++ b/src/__tests__/mock/documentTranslations.ts @@ -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', + }, +] diff --git a/src/__tests__/mock/searchDocument.ts b/src/__tests__/mock/searchDocument.ts index 03856a5..fe2cd7d 100644 --- a/src/__tests__/mock/searchDocument.ts +++ b/src/__tests__/mock/searchDocument.ts @@ -285,7 +285,7 @@ export const searchDocumentMock: SkuDocument = { CategoryId: null, IsFilter: false, Position: 1, - IsStockKeppingUnit: false, + IsStockKeppingUnit: true, IsActive: true, IsSideMenuLinkActive: false, IsTopMenuLinkActive: null, diff --git a/src/__tests__/mock/searchProduct.ts b/src/__tests__/mock/searchProduct.ts index 56b55bc..e674e95 100644 --- a/src/__tests__/mock/searchProduct.ts +++ b/src/__tests__/mock/searchProduct.ts @@ -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', diff --git a/src/convertSearchDocument.ts b/src/convertSearchDocument.ts index 9aec907..aa57b01 100644 --- a/src/convertSearchDocument.ts +++ b/src/convertSearchDocument.ts @@ -1,11 +1,5 @@ import { dateToTicks, getPriceRange, getTranslationInfo, objToNameValue } from './utils' -interface TranslatedProperty { - field: string - context: string - translation: string -} - const searchableClustersFromDocument = ( ProductClusterNames: Record, ProductClusterSearchableIds: number[] diff --git a/src/typings/global.ts b/src/typings/global.ts index b2b6315..2dd68a9 100644 --- a/src/typings/global.ts +++ b/src/typings/global.ts @@ -887,4 +887,10 @@ declare global { } type IndexingType = 'API' | 'XML' + + interface TranslatedProperty { + field: string + context: string + translation: string + } }