Skip to content

Commit

Permalink
[FIX] test: undeterministic test in spreadsheet.test.ts
Browse files Browse the repository at this point in the history
Before this fix the test "Can instantiate a spreadsheet with a given client id-name"
failed frequently on my machine.

Using jest's fake timers for those tests (timers are used in the debounce) fixed the issue

Task: 3618912
Part-of: #3364
  • Loading branch information
VincentSchippefilt committed Jan 25, 2024
1 parent 0060f5a commit 1e377d5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/helpers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export function getItemId<T>(item: T, itemsDic: { [id: number]: T }) {
*/
export function debounce(func: Function, wait: number, immediate?: boolean): Function {
let timeout;
return function (this: any) {
return function (this: any): void {
const context = this;
const args = arguments;
function later() {
Expand Down
2 changes: 1 addition & 1 deletion tests/components/__snapshots__/spreadsheet.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Spreadsheet simple rendering snapshot 1`] = `
exports[`Simple Spreadsheet Component simple rendering snapshot 1`] = `
<div
class="o-spreadsheet"
style="grid-template-rows: 63px auto 37px"
Expand Down
24 changes: 22 additions & 2 deletions tests/components/spreadsheet.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model } from "../../src";
import { Spreadsheet } from "../../src/components";
import { DEFAULT_CELL_HEIGHT } from "../../src/constants";
import { DEBOUNCE_TIME, DEFAULT_CELL_HEIGHT } from "../../src/constants";
import { args, functionRegistry } from "../../src/functions";
import { toZone } from "../../src/helpers";
import { OPEN_CF_SIDEPANEL_ACTION } from "../../src/registries";
Expand Down Expand Up @@ -52,7 +52,14 @@ Object.defineProperty(navigator, "clipboard", {
configurable: true,
});

describe("Spreadsheet", () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});
describe("Simple Spreadsheet Component", () => {
test("simple rendering snapshot", async () => {
({ model, parent, fixture } = await mountSpreadsheet({
model: new Model({ sheets: [{ id: "sh1" }] }),
Expand Down Expand Up @@ -197,6 +204,19 @@ describe("Spreadsheet", () => {
const client = { id: "alice", name: "Alice" };
({ model } = await mountSpreadsheet({ model: new Model({}, { client }) }));
expect(model.getters.getClient()).toEqual(client);

// Validate that after the move debounce has run, the client has a position ad
// additional property
jest.advanceTimersByTime(DEBOUNCE_TIME + 1);
expect(model.getters.getClient()).toEqual({
id: "alice",
name: "Alice",
position: {
col: 0,
row: 0,
sheetId: "Sheet1",
},
});
});

test("Spreadsheet detects frozen panes that exceed the limit size at start", async () => {
Expand Down

0 comments on commit 1e377d5

Please sign in to comment.