Skip to content

Commit

Permalink
fix: fix unit tests typings
Browse files Browse the repository at this point in the history
- This fixes all remaining type errors in unit tests while also adding
a step in the pipeline to check the types for unit tests as well.
- Fix recentlyViewed isLoading reducer which was not returning false
when handling error action types.
- Fix some exported types.
- Changed renderContent to return a Fragment instead of an array of
elements.
  • Loading branch information
Bruno Alexandre Oliveira authored and boliveira committed Dec 9, 2022
1 parent 718fe08 commit decf0c0
Show file tree
Hide file tree
Showing 259 changed files with 2,889 additions and 2,066 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@
"overrides": [
//Disable all tsdoc checking in test files
{
"files": ["*.test.js"],
"files": ["*.test.[t|j]s*", "*.fixtures.[t|j]s*"],
"rules": {
"tsdoc/syntax": "off",
// We use const xxx = () => {} many times in tests.
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-var-requires": "off"
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/no-non-null-assertion": "off"
}
},
{
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"prepare": "husky install",
"yalc:publish": "scripts/yalc-publish.sh",
"yalc:push": "scripts/yalc-push.sh",
"ci:types": "lerna run --concurrency 4 --no-bail --parallel --scope \"@farfetch/*\" ci:types",
"ci:types": "yarn ci:types:runtime && yarn ci:types:tests",
"ci:types:runtime": "lerna run --concurrency 4 --no-bail --parallel --scope \"@farfetch/*\" ci:types",
"ci:types:tests": "tsc -p tsconfig.ci.tests.json",
"ci:release": "scripts/release.sh"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build:copy-package-json": "node ../../scripts/copy-package-json.js",
"prebuild": "yarn clean",
"clean": "rimraf dist tsconfig.release.tsbuildinfo",
"ci:types": "tsc -p tsconfig.ci.json",
"ci:types": "tsc -p tsconfig.ci.runtime.json",
"prepack": "yarn build:copy-package-json"
},
"repository": {
Expand Down
27 changes: 9 additions & 18 deletions packages/analytics/src/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Integration } from '../integrations';
import { logger } from '../utils';
import { mockUsersResponse } from 'tests/__fixtures__/users';
import { PACKAGE_NAME } from '../utils/constants';
import Analytics from '../';
import eventTypes from '../types/eventTypes';
Expand Down Expand Up @@ -235,7 +236,7 @@ describe('analytics', () => {
});

it('should log an error when calling `analytics.setUser`', async () => {
await analytics.setUser(123456, {});
await analytics.setUser(123456, mockUsersResponse);

expect(loggerErrorSpy).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -631,14 +632,13 @@ describe('analytics', () => {
describe('User', () => {
it("Should return user's data", async () => {
const userId = 1;
const traits = { isGuest: false };

await analytics.setUser(userId, traits);
await analytics.setUser(userId, mockUsersResponse);

const userData = (await analytics.user()) as UserData;

expect(userData['id']).toEqual(userId);
expect(userData['traits']).toEqual(traits);
expect(userData['traits']).toEqual(mockUsersResponse);
});

it("Should return user's empty data structure if there's no data on storage", async () => {
Expand Down Expand Up @@ -668,22 +668,18 @@ describe('analytics', () => {
expect(user.localId).not.toBeNull();

const userId = 12345678;
const traits = {
name: 'Foo',
email: '[email protected]',
};

await analytics.setUser(userId, traits);
await analytics.setUser(userId, mockUsersResponse);

user = (await analytics.user()) as UserData;

expect(user.id).toEqual(userId);
expect(user.traits).toEqual(traits);
expect(user.traits).toEqual(mockUsersResponse);
expect(user.localId).not.toBeNull();
});

it('Should anonymize an user', async () => {
await analytics.setUser(12345678, { name: 'Dummy' });
await analytics.setUser(12345678, mockUsersResponse);

const currentLocalId = ((await analytics.user()) as UserData).localId;

Expand Down Expand Up @@ -733,7 +729,7 @@ describe('analytics', () => {
it('Should log an error message if setUser method throws', async () => {
await setupAnalyticsWithFaultyStorage();

await analytics.setUser(12, { isGuest: false });
await analytics.setUser(12, mockUsersResponse);

expect(loggerErrorSpy).toHaveBeenCalledTimes(1);
expect(loggerErrorSpy).toBeCalledWith(
Expand Down Expand Up @@ -776,12 +772,7 @@ describe('analytics', () => {
);

const userId = 12345678;
const traits = {
name: 'Foo',
email: '[email protected]',
};

await analytics.setUser(userId, traits);
await analytics.setUser(userId, mockUsersResponse);

expect(spyIntegration1).toBeCalled();
expect(spyIntegration2).toBeCalled();
Expand Down
11 changes: 4 additions & 7 deletions packages/analytics/src/__tests__/user.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mockUsersResponse } from 'tests/__fixtures__/users';
import { StorageWrapper } from '../utils';
import TestStorage from 'test-storage';
import User from '../User';
Expand Down Expand Up @@ -32,7 +33,7 @@ describe('User', () => {
});

it('Should delete the user', async () => {
await userInstance.set(123123, { name: 'foo' });
await userInstance.set(123123, mockUsersResponse);
const beforeAnonymizeLocalId = await userInstance.localId();

await userInstance.anonymize();
Expand All @@ -46,17 +47,13 @@ describe('User', () => {

it('Should identify an user with parameters', async () => {
const userId = 12345678;
const traits = {
name: 'Foo',
email: '[email protected]',
};

await userInstance.set(userId, traits);
await userInstance.set(userId, mockUsersResponse);

const data = await userInstance.get();

expect(data.id).toBe(userId);
expect(data.traits).toMatchObject(traits);
expect(data.traits).toMatchObject(mockUsersResponse);
});

it('Should create a local ID and store it on the storage', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { pageEventsData } from 'tests/__fixtures__/analytics';
import { pageTypes } from '../../..';
import { userGenderValuesMapper } from '../definitions';
import mockedUuid from './mocked_uuid';
import type { DefaultPageFixturesResult } from 'tests/__fixtures__/analytics/page';

const pageMockData = pageEventsData[pageTypes.HOMEPAGE];
const pageMockData = pageEventsData[
pageTypes.HOMEPAGE
] as DefaultPageFixturesResult;

const fixtures = {
clientId: pageMockData.context.clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { pageEventsData } from 'tests/__fixtures__/analytics';
import { pageTypes } from '../../..';
import { userGenderValuesMapper } from '../definitions';
import mockedUuid from './mocked_uuid';
import type { DefaultPageFixturesResult } from 'tests/__fixtures__/analytics/page';

const pageMockData = pageEventsData[pageTypes.HOMEPAGE];
const pageMockData = pageEventsData[
pageTypes.HOMEPAGE
] as DefaultPageFixturesResult;

const fixtures = {
clientId: pageMockData.context.clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
expectedPagePayloadWeb as mockExpectedPagePayloadWeb,
expectedTrackPayload as mockExpectedTrackPayload,
} from '../__fixtures__';
import { mockUsersResponse } from 'tests/__fixtures__/users';
import {
OPTION_HTTP_CLIENT,
OPTION_SEARCH_QUERY_PARAMETERS,
Expand Down Expand Up @@ -262,6 +263,7 @@ describe('Omnitracking', () => {

it('Should return the formatted object for the `ListingPageVisited` event', async () => {
const data = generateMockData({
// @ts-expect-error Missing properties from EventContext just to facilitate testing
context: {
web: {
window: {
Expand All @@ -270,7 +272,7 @@ describe('Omnitracking', () => {
},
},
},
} as unknown as EventContext,
} as EventContext,
});

await omnitracking.track(data);
Expand All @@ -284,6 +286,7 @@ describe('Omnitracking', () => {

it('Should return the formatted object for the `ProductPageVisited` event', async () => {
const data = generateMockData({
// @ts-expect-error Missing properties from EventContext just to facilitate testing
context: {
web: {
window: {
Expand All @@ -292,7 +295,7 @@ describe('Omnitracking', () => {
},
},
},
} as unknown as EventContext,
} as EventContext,
});

await omnitracking.track(data);
Expand All @@ -306,6 +309,7 @@ describe('Omnitracking', () => {

it('Should return the formatted object for the `CheckoutVisited` event', async () => {
const data = generateMockData({
// @ts-expect-error Missing properties from EventContext just to facilitate testing
context: {
web: {
window: {
Expand All @@ -314,7 +318,7 @@ describe('Omnitracking', () => {
},
},
},
} as unknown as EventContext,
} as EventContext,
});

await omnitracking.track(data);
Expand Down Expand Up @@ -346,6 +350,7 @@ describe('Omnitracking', () => {
user: {
id: 123123,
traits: {
...mockUsersResponse,
isGuest: true,
},
localId: 'dummy',
Expand Down Expand Up @@ -935,7 +940,7 @@ describe('Omnitracking', () => {
// the mockedUuid so the uniqueViewId changes
// on the next omnitracking.track call with a page event.
// Remember that v4 is jest.fn() in this test file.
(uuid.v4 as unknown as jest.Mock<string>).mockImplementation(
(uuid.v4 as jest.Mock<string>).mockImplementation(
() => '981945ad-b9d4-4c21-b3b0-2764b31bdc43',
);

Expand All @@ -949,9 +954,7 @@ describe('Omnitracking', () => {
// After this, we need to restore the mock for v4
// to use the original implementation in other tests
// that may be defined after this one.
(uuid.v4 as unknown as jest.Mock<string>).mockImplementation(
() => mockedUuid,
);
(uuid.v4 as jest.Mock<string>).mockImplementation(() => mockedUuid);

expect(lastPayload.parameters.uniqueViewId).not.toBe(newUniqueViewId);
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ Object {

exports[`Omnitracking track events definitions \`product-listing\` return should match the snapshot 1`] = `
Object {
"lineItems": "[{\\"productId\\":12345678,\\"itemPromotion\\":1,\\"designerName\\":\\"designer name\\",\\"category\\":\\"shoes\\",\\"itemFullPrice\\":1,\\"sizeID\\":1}]",
"lineItems": "[{\\"productId\\":\\"12345678\\",\\"itemPromotion\\":1,\\"designerName\\":\\"designer name\\",\\"category\\":\\"shoes\\",\\"itemFullPrice\\":1,\\"sizeID\\":1}]",
"viewSubType": "Listing",
"viewType": "Listing",
}
Expand Down Expand Up @@ -1125,7 +1125,7 @@ Array [

exports[`Omnitracking track events definitions \`wishlist\` return should match the snapshot 1`] = `
Object {
"lineItems": "[{\\"productId\\":12122479,\\"itemPromotion\\":1,\\"designerName\\":\\"Saint Laurent\\",\\"category\\":\\"Clothing\\",\\"itemFullPrice\\":2,\\"sizeID\\":\\"20\\",\\"itemQuantity\\":13,\\"promoCode\\":\\"EASTER2022\\",\\"storeID\\":20000},{\\"productId\\":12122479,\\"itemPromotion\\":1,\\"designerName\\":\\"Saint Laurent\\",\\"category\\":\\"Clothing\\",\\"itemFullPrice\\":2,\\"sizeID\\":\\"21\\",\\"itemQuantity\\":13,\\"promoCode\\":\\"EASTER2022\\",\\"storeID\\":20000}]",
"lineItems": "[{\\"productId\\":\\"12122479\\",\\"itemPromotion\\":1,\\"designerName\\":\\"Saint Laurent\\",\\"category\\":\\"Clothing\\",\\"itemFullPrice\\":2,\\"sizeID\\":\\"20\\",\\"itemQuantity\\":13,\\"promoCode\\":\\"EASTER2022\\",\\"storeID\\":20000},{\\"productId\\":\\"12122479\\",\\"itemPromotion\\":1,\\"designerName\\":\\"Saint Laurent\\",\\"category\\":\\"Clothing\\",\\"itemFullPrice\\":2,\\"sizeID\\":\\"21\\",\\"itemQuantity\\":13,\\"promoCode\\":\\"EASTER2022\\",\\"storeID\\":20000}]",
"viewSubType": "Wishlist",
"viewType": "Wishlist",
"wishlistQuantity": 26,
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build:copy-package-json": "node ../../scripts/copy-package-json.js",
"prebuild": "yarn clean",
"clean": "rimraf dist tsconfig.release.tsbuildinfo",
"ci:types": "tsc -p tsconfig.ci.json",
"ci:types": "tsc -p tsconfig.ci.runtime.json",
"prepack": "yarn build:copy-package-json"
},
"repository": {
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/bags/types/bagItem.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type BagItem = {
totalDiscountValue: number | null;
formattedTotalDiscountValue: string;
isProductOffer: boolean;
promotionEvaluationItemId: string | null;
};
type: number;
price: Price;
Expand Down
7 changes: 7 additions & 0 deletions packages/client/src/categories/types/category.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ export type Category = {
export type ProductCategory = Omit<Category, 'uuid' | 'gender'> & {
gender: GenderCode;
};

export type ProductCategoryWithGenderDescription = Omit<
ProductCategory,
'gender'
> & {
gender: Gender;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { rest, RestHandler } from 'msw';
import type { GetCheckoutOrderOperationsResponse } from '../types/getCheckoutOrderOperations.types';
import type { CheckoutOrderOperations } from '../types/getCheckoutOrderOperations.types';

const path = '/api/checkout/v1/orders/:id/operations';

const fixtures = {
success: (response: GetCheckoutOrderOperationsResponse): RestHandler =>
success: (response: CheckoutOrderOperations): RestHandler =>
rest.get(path, async (_req, res, ctx) =>
res(ctx.status(200), ctx.json(response)),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('checkout client', () => {

it('should handle a client request successfully', async () => {
const response: CheckoutOrderCharge = {
chargeId: 'string',
id: 'string',
status: ChargeStatus.Processing,
redirectUrl: 'string',
returnUrl: 'string',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deliveryBundleId, id } from 'tests/__fixtures__/checkout';
import client from '../../helpers/client';
import fixtures from '../__fixtures__/getCheckoutOrderDeliveryBundleProvisioning.fixtures';
import mswServer from '../../../tests/mswServer';
import type { GetCheckoutOrderDeliveryBundleProvisioningResponse } from '../types';
import type { ItemDeliveryProvisioning } from '../types';

describe('getCheckoutOrderDeliveryBundleProvisioning', () => {
beforeEach(() => jest.clearAllMocks());
Expand All @@ -13,7 +13,7 @@ describe('getCheckoutOrderDeliveryBundleProvisioning', () => {
const urlToBeCalled = `/checkout/v1/orders/${id}/deliveryBundles/${deliveryBundleId}/itemDeliveryProvisioning`;

it('should handle a client request successfully', async () => {
const response: GetCheckoutOrderDeliveryBundleProvisioningResponse = [
const response: ItemDeliveryProvisioning[] = [
{
itemId: 0,
provisioning: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deliveryBundleId, id, upgradeId } from 'tests/__fixtures__/checkout';
import client from '../../helpers/client';
import fixtures from '../__fixtures__/getCheckoutOrderDeliveryBundleUpgradeProvisioning.fixtures';
import mswServer from '../../../tests/mswServer';
import type { GetCheckoutOrderDeliveryBundleUpgradeProvisioningResponse } from '../types';
import type { ItemDeliveryProvisioning } from '../types';

describe('getCheckoutOrderDeliveryBundleUpgradeProvisioning', () => {
beforeEach(() => jest.clearAllMocks());
Expand All @@ -13,22 +13,21 @@ describe('getCheckoutOrderDeliveryBundleUpgradeProvisioning', () => {
const urlToBeCalled = `/checkout/v1/orders/${id}/deliveryBundles/${deliveryBundleId}/upgrades/${upgradeId}/itemDeliveryProvisioning`;

it('should handle a client request successfully', async () => {
const response: GetCheckoutOrderDeliveryBundleUpgradeProvisioningResponse =
[
{
itemId: 0,
provisioning: [
{
merchantId: 0,
merchantLocationId: 0,
quantity: 0,
deliveryDateEstimateMinimum: '2021-07-22T13:20:58.648Z',
deliveryDateEstimateMaximum: '2021-07-22T13:20:58.648Z',
deliveryDateEstimate: '2021-07-22T13:20:58.648Z',
},
],
},
];
const response: ItemDeliveryProvisioning[] = [
{
itemId: 0,
provisioning: [
{
merchantId: 0,
merchantLocationId: 0,
quantity: 0,
deliveryDateEstimateMinimum: '2021-07-22T13:20:58.648Z',
deliveryDateEstimateMaximum: '2021-07-22T13:20:58.648Z',
deliveryDateEstimate: '2021-07-22T13:20:58.648Z',
},
],
},
];

mswServer.use(fixtures.success(response));

Expand Down
Loading

0 comments on commit decf0c0

Please sign in to comment.