-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
packages/pluggableWidgets/datagrid-web/src/__tests__/DatasourceController.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |