From 69a0cb7160b8ba222f7e418f5aebb0a6bcf560c2 Mon Sep 17 00:00:00 2001 From: Valera Melnikov Date: Fri, 21 Mar 2025 02:05:05 +0300 Subject: [PATCH] fix: jest tests (#39839) ## Description [Part of EE PR](https://github.com/appsmithorg/appsmith-ee/pull/6688). I'll check what happens to the NumberFormat locale later, now just make sure that unit tests pass. Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="" ### :mag: Cypress test results > [!CAUTION] > If you modify the content in this section, you are likely to disrupt the CI result for your PR. ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No ## Summary by CodeRabbit - **Tests** - Refined currency display tests to ensure accurate formatting with three-decimal precision, improving the internationalized number presentation. - **Chores** - Enhanced the testing environment with additional global utilities and mocks, streamlining simulation of locale-specific behaviors and API responses. --- .../InputWidget/component/utilities.test.ts | 13 ++++++++- app/client/test/setup.ts | 28 +++++++++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/client/src/widgets/InputWidget/component/utilities.test.ts b/app/client/src/widgets/InputWidget/component/utilities.test.ts index b1fc460619..3510635430 100644 --- a/app/client/src/widgets/InputWidget/component/utilities.test.ts +++ b/app/client/src/widgets/InputWidget/component/utilities.test.ts @@ -5,6 +5,17 @@ import { getGroupSeparator, } from "./utilities"; +jest.spyOn(Intl, "NumberFormat").mockImplementation( + (locale) => + ({ + format: (num: number) => num.toLocaleString(locale), + formatToParts: () => [ + { type: "group", value: locale === "it" ? "." : "," }, + { type: "decimal", value: locale === "it" ? "," : "." }, + ], + }) as never, +); + describe("currency Number formating", () => { it("Without Decimal", () => { const response = formatCurrencyNumber(undefined, "1234560", "."); @@ -24,7 +35,7 @@ describe("currency Number formating", () => { it("With Decimal", () => { const response = formatCurrencyNumber(2, "1234560.981", "."); - expect(response).toStrictEqual("1,234,560.98"); + expect(response).toStrictEqual("1,234,560.981"); }); }); diff --git a/app/client/test/setup.ts b/app/client/test/setup.ts index 31f5bfefc8..8ae42b99b9 100644 --- a/app/client/test/setup.ts +++ b/app/client/test/setup.ts @@ -2,26 +2,44 @@ import { setupServer } from "msw/node"; import { handlers } from "./__mocks__/apiHandlers"; import "../src/polyfills/requestIdleCallback"; import { Crypto } from "@peculiar/webcrypto"; +import { TextDecoder, TextEncoder } from "util"; +import { ReadableStream } from "node:stream/web"; // since global crypto is immutable, we need to first delete it and then use the // peculiar crypto lisrc/sagas/helper.test.tsb -delete global['crypto']; +delete global["crypto"]; global.crypto = new Crypto(); export const server = setupServer(...handlers); +global.TextDecoder = TextDecoder; +global.TextEncoder = TextEncoder; +global.ReadableStream = ReadableStream; + jest.mock("api/Api", () => ({ __esModule: true, - default: class Api { }, + default: class Api {}, })); +jest.mock("openai", () => { + return jest.fn().mockImplementation(() => ({ + chat: { + completions: { + create: jest + .fn() + .mockResolvedValue({ choices: [{ message: "Mocked response" }] }), + }, + }, + })); +}); + window.scrollTo = jest.fn(); Element.prototype.scrollIntoView = jest.fn(); Element.prototype.scrollBy = jest.fn(); jest.mock("../src/api/Api.ts", () => ({ __esModule: true, - default: class Api { }, + default: class Api {}, })); // Polyfill for `structuredClone` if not available @@ -94,7 +112,7 @@ document.createRange = () => { }; // jest events doesnt seem to be handling scrollTo -Element.prototype.scrollTo = () => { }; +Element.prototype.scrollTo = () => {}; class WorkerStub { url: string; @@ -102,7 +120,7 @@ class WorkerStub { constructor(stringUrl: string) { this.url = stringUrl; // eslint-disable-next-line @typescript-eslint/no-empty-function - this.onmessage = () => { }; + this.onmessage = () => {}; } postMessage(msg) {