Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thalytafabrine committed Feb 28, 2024
1 parent ead8e36 commit 19d3447
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 11 deletions.
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
6 changes: 0 additions & 6 deletions src/convertSearchDocument.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { dateToTicks, getPriceRange, getTranslationInfo, objToNameValue } from './utils'

interface TranslatedProperty {
field: string
context: string
translation: string
}

const searchableClustersFromDocument = (
ProductClusterNames: Record<string, string>,
ProductClusterSearchableIds: number[]
Expand Down
6 changes: 6 additions & 0 deletions src/typings/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -887,4 +887,10 @@ declare global {
}

type IndexingType = 'API' | 'XML'

interface TranslatedProperty {
field: string
context: string
translation: string
}
}

0 comments on commit 19d3447

Please sign in to comment.