forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 2
Proof of concept - use vitest instead of mocha/should #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jameslounds
wants to merge
12
commits into
feat/typescript-migration
Choose a base branch
from
proof-of-concept/vitest
base: feat/typescript-migration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
32f41ff
remove false jsdoc annotation
jameslounds dc2997f
migrate to vitest; migrate boluswizardpreview test to vitest
jameslounds 318cc1d
migrate boluswizardpreview plugin, plugins/index, pluginbase, sandbox…
jameslounds 97d993e
add `pluginbase.test.js` - a test which uses `jsdom` to run the actua…
jameslounds 5086c44
include @testing-library/jest-dom/vitest in vite `test.setupFiles` di…
jameslounds eec609b
just import @testing-library/jest-dom matchers where needed (helps wi…
jameslounds 41d8a99
add test for pluginbase tooltips, test *all* plugin types have their …
jameslounds bc79aa6
migrate `sandbox.test` to vitest with typescript
jameslounds 358fd7e
use baseEnv in `boluswizardpreview.test`
jameslounds 505289a
migrate `profileeditor.test` to vitest with happy-dom (jsdom doesn't …
jameslounds 214613f
actually reset document on each test rather than appending the html e…
jameslounds 849af2a
make frontend actually run
jameslounds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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,98 @@ | ||
| import { vi } from "vitest"; | ||
|
|
||
| export const ajaxMock = vi.fn(); | ||
| export const mockState: Map< | ||
| RegExp | string, | ||
| { data: any; error?: never } | { error: any; data?: never } | ||
| > = new Map(); | ||
|
|
||
| type ValueOfMap<T> = T extends Map<any, infer G> ? G : never; | ||
|
|
||
| function mockImplementation( | ||
| opts: JQuery.AjaxSettings & { url: string } | ||
| ): JQuery.jqXHR; | ||
| function mockImplementation( | ||
| maybeOpts: string, | ||
| opts?: JQuery.AjaxSettings | ||
| ): JQuery.jqXHR; | ||
| function mockImplementation( | ||
| maybeOpts: (JQuery.AjaxSettings & { url: string }) | string, | ||
| opts?: JQuery.AjaxSettings | ||
| ): JQuery.jqXHR { | ||
| const url = typeof maybeOpts === "string" ? maybeOpts : maybeOpts.url; | ||
| opts = typeof maybeOpts === "string" ? opts : maybeOpts; | ||
|
|
||
| const data = [...mockState.entries()].find(([matcher, data]) => | ||
| url.match(matcher) | ||
| )?.[1]; | ||
|
|
||
| if (!data) throw Error(`[mockAjax] route ${url} not handled`); | ||
| class AjaxReturnMock implements JQuery.jqXHR { | ||
| constructor(private data: ValueOfMap<typeof mockState>) {} | ||
|
|
||
| state() { | ||
| return "resolved" as "resolved"; | ||
| } | ||
| done(callback: (d: any) => void) { | ||
| if (this.data.data) { | ||
| callback(this.data.data); | ||
| } | ||
| return this; | ||
| } | ||
| fail(callback: (d: any) => void) { | ||
| if (this.data.error) { | ||
| callback(this.data.error); | ||
| } | ||
| return this; | ||
| } | ||
| statusCode() { | ||
| return this.data.error ? 500 : 200; | ||
| } | ||
| get responseText() { | ||
| return this.data.error ? "error" : "OK"; | ||
| } | ||
| abort() {} | ||
| always(callback: (d: any) => void) { | ||
| callback(this.data.data || this.data.error); | ||
| return this; | ||
| } | ||
| progress() { | ||
| return this; | ||
| } | ||
| promise() { | ||
| return this; | ||
| } | ||
| pipe() { | ||
| return this; | ||
| } | ||
| then() { | ||
| return this; | ||
| } | ||
| catch() { | ||
| return this; | ||
| } | ||
| getAllResponseHeaders() { | ||
| return ""; | ||
| } | ||
| getResponseHeader() { | ||
| return ""; | ||
| } | ||
| overrideMimeType() { | ||
| return ""; | ||
| } | ||
| get readyState() { | ||
| return 1; | ||
| } | ||
| setRequestHeader() { | ||
| return this; | ||
| } | ||
| get status() { | ||
| return this.data.error ? 500 : 200; | ||
| } | ||
| get statusText() { | ||
| return this.data.error ? "error" : "OK"; | ||
| } | ||
| } | ||
| return new AjaxReturnMock(data); | ||
| } | ||
| ajaxMock.mockImplementation(mockImplementation); |
This file contains hidden or 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,42 @@ | ||
| export const callbacksByRouteByEvent = { | ||
| default: {} as Record<string, (...args: any) => void>, | ||
| "/alarm": {} as Record<string, (...args: any) => void>, | ||
| }; | ||
| export const emitResponsesByRouteByEvent = { | ||
| default: {} as Record<string, any>, | ||
| "/alarm": {} as Record<string, any>, | ||
| }; | ||
| export const sockerIOClientMock = { | ||
| connect: ( | ||
| routeOrOpts: keyof typeof callbacksByRouteByEvent | Record<string, any>, | ||
| opts?: Record<string, any> | ||
| ) => { | ||
| const route = | ||
| typeof routeOrOpts === "undefined" || typeof routeOrOpts === "object" | ||
| ? "default" | ||
| : routeOrOpts; | ||
| switch (route) { | ||
| case "default": | ||
| return defaultSocket; | ||
| case "/alarm": | ||
| return alarmSocket; | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| const makeSocket = (route: keyof typeof callbacksByRouteByEvent) => ({ | ||
| on(event: string, callback: (...args: any) => void) { | ||
| callbacksByRouteByEvent[route][event] = callback; | ||
| }, | ||
| emit(event: string, data: any, callback: (...args: any[]) => void) { | ||
| callback(emitResponsesByRouteByEvent[route][event]); | ||
| }, | ||
|
|
||
| trigger(event: string, ...args: any[]) { | ||
| const callback = callbacksByRouteByEvent[route][event]; | ||
| if (callback) callback(...args); | ||
| }, | ||
| }); | ||
|
|
||
| export const defaultSocket = makeSocket("default"); | ||
| export const alarmSocket = makeSocket("/alarm"); |
This file contains hidden or 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,33 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
| import setupBrowser from "./setup/browser"; | ||
|
|
||
| describe("pluginbase", () => { | ||
| it("updates major pill text correctly", async () => { | ||
| await setupBrowser(); | ||
|
|
||
| const bgStatusBefore = document.querySelector(".bgStatus"); | ||
| const minorPillsBefore = document.querySelector(".minorPills"); | ||
|
|
||
| window.Nightscout.client.ctx.pluginBase.updatePillText( | ||
| { | ||
| name: "fake", | ||
| label: "Insulin-on-Board", | ||
| pluginType: "pill-major", | ||
| }, | ||
| { | ||
| value: "123", | ||
| label: "TEST", | ||
| info: [{ label: "Label", value: "Value" }], | ||
| } | ||
| ); | ||
|
|
||
| expect(document.querySelector(".majorPills")).toHaveTextContent("TEST123"); | ||
|
|
||
| expect(document.querySelector(".minorPills")).toBe(minorPillsBefore); | ||
| expect(document.querySelector(".bgStatus")).toBe(bgStatusBefore); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Original File