PromucFlow_constructor/app/client/test/setup.ts
Valera Melnikov 69a0cb7160
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 -->
2025-03-21 02:05:05 +03:00

132 lines
3.1 KiB
TypeScript

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"];
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 {},
}));
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 {},
}));
// Polyfill for `structuredClone` if not available
// This is needed for eslint jest tests
if (typeof global.structuredClone === "undefined") {
global.structuredClone = (obj) => {
return JSON.parse(JSON.stringify(obj));
};
}
beforeAll(() => {
window.IntersectionObserver = jest
.fn()
.mockImplementation((fn: (entry: any) => any) => {
return {
observe: () => {
fn([
{
isIntersecting: true,
boundingClientRect: {
top: 64,
left: 293,
},
intersectionRect: {
width: 1296,
height: 424,
top: 64,
left: 293,
},
},
]);
},
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
window.ResizeObserver = jest.fn().mockImplementation(() => {
return {
observe: jest.fn(),
unobserve: jest.fn(),
disconnect: jest.fn(),
};
});
});
// establish API mocking before all tests
beforeAll(() => server.listen());
// reset any request handlers that are declared as a part of our tests
// (i.e. for testing one-time error scenarios)
afterEach(() => server.resetHandlers());
// clean up once the tests are done
afterAll(() => server.close());
// popper.js fix for jest tests
document.createRange = () => {
const range = new Range();
range.getBoundingClientRect = jest.fn();
range.getClientRects = () => {
return {
item: () => null,
length: 0,
[Symbol.iterator]: jest.fn(),
};
};
return range;
};
// jest events doesnt seem to be handling scrollTo
Element.prototype.scrollTo = () => {};
class WorkerStub {
url: string;
onmessage: CallableFunction;
constructor(stringUrl: string) {
this.url = stringUrl;
// eslint-disable-next-line @typescript-eslint/no-empty-function
this.onmessage = () => {};
}
postMessage(msg) {
this.onmessage(msg);
}
}
window.Worker = WorkerStub as any;