Skip to content

Commit

Permalink
feat: add refresh tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iobuhov committed Feb 13, 2025
1 parent f59d44c commit 9f2af4c
Showing 1 changed file with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { GateProvider } from "@mendix/widget-plugin-mobx-kit/GateProvider";
import { ReactiveControllerHost } from "@mendix/widget-plugin-mobx-kit/main";
import { list } from "@mendix/widget-plugin-test-utils";
import { ListValue } from "mendix";
import { DatasourceController } from "../controllers/DatasourceController";

describe("DatasourceController loading states", () => {
let controller: DatasourceController;
let datasource: ListValue;
let provider: GateProvider<{ datasource: ListValue }>;

beforeEach(() => {
const host = { addController: jest.fn() } as unknown as ReactiveControllerHost;
provider = new GateProvider({ datasource: list.loading() });
controller = new DatasourceController(host, { gate: provider.gate });
});

describe("when datasource is loading", () => {
beforeEach(() => {
datasource = list.loading();
provider.setProps({ datasource });
});

it("isLoading returns true by default", () => {
expect(controller.isLoading).toBe(true);
});

it("refresh has no effect if ds is loading", () => {
expect(provider.gate.props.datasource.status).toBe("loading");
controller.refresh();
expect(controller.isRefreshing).toBe(false);
});

it("refresh rise isRefreshing once ds become loading", () => {
provider.setProps({ datasource: list(0) });
expect(provider.gate.props.datasource.status).toBe("available");
controller.refresh();
expect(controller.isRefreshing).toBe(false);
provider.setProps({ datasource: list.loading() });
expect(provider.gate.props.datasource.status).toBe("loading");
expect(controller.isRefreshing).toBe(true);
expect(controller.isLoading).toBe(false);
});

it("isLoadingMore returns true after setLimit call", () => {
controller.setLimit(20);
expect(controller.isLoadingMore).toBe(true);
expect(controller.isLoading).toBe(false);
});
});

describe("when datasource is not loading", () => {
beforeEach(() => {
datasource = list(0);
provider.setProps({ datasource });
});

it("all loading states return false", () => {
expect(controller.isLoading).toBe(false);
expect(controller.isRefreshing).toBe(false);
expect(controller.isLoadingMore).toBe(false);
});

it("triggers refresh when called", () => {
controller.refresh();
expect(datasource.reload).toHaveBeenCalled();
});

it("triggers setLimit when called", () => {
controller.setLimit(20);
expect(datasource.setLimit).toHaveBeenCalledWith(20);
});
});
});

0 comments on commit 9f2af4c

Please sign in to comment.