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=""

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]  
> If you modify the content in this section, you are likely to disrupt
the CI result for your PR.

<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [x] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Valera Melnikov 2025-03-21 02:05:05 +03:00 committed by GitHub
parent 55624bc499
commit 69a0cb7160
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 6 deletions

View File

@ -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");
});
});

View File

@ -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) {