Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mzanoni committed Nov 6, 2023
1 parent d467e57 commit 521e733
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 25 deletions.
5 changes: 2 additions & 3 deletions packages/integrations/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
indent: ['error', 4],
quotes: ['error', 'single'],
semi: 0,
semi: ['error', 'always'],
'no-extra-semi': 0,
'comma-dangle': ['error', 'always-multiline'], // Reasoning behind using dangling commas -> https://github.com/airbnb/javascript#commas--dangling
'no-useless-constructor': 'off',
Expand All @@ -20,8 +20,7 @@ module.exports = {
'space-before-function-paren': ['error', 'never'],
},
parser: '@typescript-eslint/parser',
parserOptions: {

parserOptions: {
'ecmaVersion': 'latest',
'sourceType': 'module',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Product, MultilingualDataValue, DataValue, ProductVariant, CategoryPath, MultiCurrencyDataValue, Brand, ProductUpdate } from '@relewise/client';
import { Product, DataValue, ProductVariant, CategoryPath, Brand, ProductUpdate } from '@relewise/client';

export class ProductUpdateBuilder {
private product: Product;
Expand All @@ -22,14 +22,19 @@ export class ProductUpdateBuilder {
this.brandUpdateKind = brandUpdateKind;
}

displayName(name: MultilingualDataValue): this {
this.product.displayName = name.value;
displayName(values: {
value: string;
language: string;
}[]): this {
this.product.displayName = {
values: values.map(x => ({ text: x.value, language: { value: x.language } })),
};

return this;
}

data(data: Record<string, DataValue>): this {
this.product.data = data;
data(data: Record<string, DataValue | null>): this {
this.product.data = data as Record<string, DataValue>; // TODO remove dirty hack

return this;
}
Expand All @@ -52,14 +57,20 @@ export class ProductUpdateBuilder {
return this;
}

listPrice(listPrice: MultiCurrencyDataValue): this {
this.product.listPrice = listPrice.value;
listPrice(values: {
amount: number;
currency: string;
}[]): this {
this.product.listPrice = { values: values.map(x => ({ amount: x.amount, currency: { value: x.currency } })) };

return this;
}

salesPrice(salesPrice: MultiCurrencyDataValue): this {
this.product.salesPrice = salesPrice.value;
salesPrice(values: {
amount: number;
currency: string;
}[]): this {
this.product.salesPrice = { values: values.map(x => ({ amount: x.amount, currency: { value: x.currency } })) };

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MultilingualDataValue, DataValue, ProductVariant, MultiCurrencyDataValue } from '@relewise/client';
import { DataValue, ProductVariant } from '@relewise/client';

export class ProductVariantBuilder {
private variant: ProductVariant;
Expand All @@ -9,14 +9,19 @@ export class ProductVariantBuilder {
this.variant = { id: id };
}

displayName(name: MultilingualDataValue): this {
this.variant.displayName = name.value;
displayName(values: {
value: string;
language: string;
}[]): this {
this.variant.displayName = {
values: values.map(x => ({ text: x.value, language: { value: x.language } })),
};

return this;
}

data(data: Record<string, DataValue>): this {
this.variant.data = data;
data(data: Record<string, DataValue | null>): this {
this.variant.data = data as Record<string, DataValue>; // TODO remove dirty hack

return this;
}
Expand All @@ -27,14 +32,20 @@ export class ProductVariantBuilder {
return this;
}

listPrice(listPrice: MultiCurrencyDataValue): this {
this.variant.listPrice = listPrice.value;
listPrice(values: {
amount: number;
currency: string;
}[]): this {
this.variant.listPrice = { values: values.map(x => ({ amount: x.amount, currency: { value: x.currency } })) };

return this;
}

salesPrice(salesPrice: MultiCurrencyDataValue): this {
this.variant.salesPrice = salesPrice.value;
salesPrice(values: {
amount: number;
currency: string;
}[]): this {
this.variant.salesPrice = { values: values.map(x => ({ amount: x.amount, currency: { value: x.currency } })) };

return this;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/integrations/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function example() {

integrator.batchSize = 5;

await integrator.updateProduct(product.build())
await integrator.updateProduct(product.build());

const enable = new ProductAdministrativeActionBuilder({
language: null,
Expand Down Expand Up @@ -42,11 +42,12 @@ function createProduct(id: string, unix: number) {
id: id,
productUpdateKind: 'ReplaceProvidedProperties',
})
.displayName(DataValueFactory.multilingual([{ language: 'da', value: 'product navn' }]))
.displayName([{ language: 'da', value: 'product navn' }])
.data({
'ProductType': DataValueFactory.string('Bluse'),
'Description': DataValueFactory.string('Flot blå bluse som er top nice'),
'UnixTimeStamp': DataValueFactory.number(unix),
'Removed': null,
})
.salesPrice(DataValueFactory.multiCurrency([{ currency: 'DKK', amount: 499.95 }]));
.salesPrice([{ currency: 'DKK', amount: 499.95 }]);
}
2 changes: 1 addition & 1 deletion packages/integrations/src/integrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Integrator extends RelewiseClient {
return this.request<TrackProductAdministrativeActionRequest, void>(
'TrackProductAdministrativeActionRequest',
{
$type: 'Relewise.Client.Requests.Tracking.ProductAdministrativeAction, Relewise.Client',
$type: 'Relewise.Client.Requests.Tracking.TrackProductAdministrativeActionRequest, Relewise.Client',
administrativeAction: request,
},
options);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { test, expect } from '@jest/globals';
import { Integrator, ProductAdministrativeActionBuilder, ProductUpdateBuilder } from '../../../src';
import { DataValueFactory } from '@relewise/client';
const { npm_config_API_KEY: API_KEY, npm_config_DATASET_ID: DATASET_ID, npm_config_SERVER_URL: SERVER_URL } = process.env;

const integrator = new Integrator(DATASET_ID!, API_KEY!, { serverUrl: SERVER_URL });

const unixTimeStamp: number = Date.now();

test('Create Product', async() => {
const product = new ProductUpdateBuilder({
id: '1234',
productUpdateKind: 'ReplaceProvidedProperties',
variantUpdateKind: 'ReplaceProvidedProperties',
})
.displayName([
{ language: 'da', value: 'Toaster' },
])
.data({
'UnixTimestamp': DataValueFactory.number(unixTimeStamp),
'Description': DataValueFactory.string('Really nice product'),
'Tags': DataValueFactory.stringCollection(['fall collection', 'blue', 'good-deal']),
'InStock': DataValueFactory.boolean(true),
})
.assortments([1, 2, 3])
.brand({ id: '1', displayName: 'Relewise' })
.listPrice([{ amount: 100, currency: 'DKK' }])
.salesPrice([{ amount: 50, currency: 'DKK' }]);

await integrator.updateProduct(product.build());

const enable = new ProductAdministrativeActionBuilder({
filters: (f) => f.addProductDataFilter('UnixTimeStamp', c => c.addEqualsCondition(DataValueFactory.number(unixTimeStamp))),
productUpdateKind: 'Enable',
});
integrator.productAdministrativeAction(enable.build());

const disable = new ProductAdministrativeActionBuilder({
filters: (f) => f.addProductDataFilter('UnixTimeStamp', c => c.addEqualsCondition(DataValueFactory.number(unixTimeStamp), /* negated: */ true)),
productUpdateKind: 'Disable',
});
integrator.productAdministrativeAction(disable.build());
});

test('Batch create products', async() => {
const product = new ProductUpdateBuilder({
id: '1234',
productUpdateKind: 'ReplaceProvidedProperties',
variantUpdateKind: 'ReplaceProvidedProperties',
})
.displayName([
{ language: 'da', value: 'Toaster' },
])
.data({
'UnixTimestamp': DataValueFactory.number(unixTimeStamp),
'Description': DataValueFactory.string('Really nice product'),
'Tags': DataValueFactory.stringCollection(['fall collection', 'blue', 'good-deal']),
'InStock': DataValueFactory.boolean(true),
})
.assortments([1, 2, 3])
.brand({ id: '1', displayName: 'Relewise' })
.listPrice([{ amount: 100, currency: 'DKK' }])
.salesPrice([{ amount: 50, currency: 'DKK' }]);

const product2 = new ProductUpdateBuilder({
id: '4321',
productUpdateKind: 'ReplaceProvidedProperties',
variantUpdateKind: 'ReplaceProvidedProperties',
})
.displayName([
{ language: 'da', value: 'Laptop' },
])
.data({
'UnixTimestamp': DataValueFactory.number(unixTimeStamp),
'Description': DataValueFactory.string('Really nice product'),
'Tags': DataValueFactory.stringCollection(['fall collection', 'blue', 'good-deal']),
'InStock': DataValueFactory.boolean(true),
})
.assortments([1, 2, 3])
.brand({ id: '1', displayName: 'Relewise' })
.listPrice([{ amount: 100, currency: 'DKK' }])
.salesPrice([{ amount: 50, currency: 'DKK' }]);

await integrator.batch([product.build(), product2.build()]);
});

0 comments on commit 521e733

Please sign in to comment.