Skip to content

Commit

Permalink
Add first DOM test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathonherbert committed Jul 4, 2024
1 parent c6641b3 commit cfb5d04
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 23 deletions.
Binary file modified prosemirror-client/bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion prosemirror-client/happydom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GlobalRegistrator } from "@happy-dom/global-registrator";

GlobalRegistrator.register();
const bunFetch = fetch;
GlobalRegistrator.register();
window.fetch = bunFetch;
1 change: 1 addition & 0 deletions prosemirror-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/bun": "latest",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"shadow-dom-testing-library": "^1.11.2",
"typescript": "^5.2.2",
"vite": "^5.2.0",
"vitest": "^1.6.0"
Expand Down
19 changes: 13 additions & 6 deletions prosemirror-client/src/cqlInput/CqlInput.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { test, mock } from "bun:test";
import { test, mock, expect } from "bun:test";
import { createCqlInput } from "./CqlInput";
import { CqlService } from "../CqlService";
import { TestCqlService } from "../services/TestCqlService";
import example from "./fixtures/responses/example.json";
import { findByTestId } from "@testing-library/dom";

mock.module("../CqlService", () => ({}));

const testCqlService = new CqlService("http://localhost");
const testCqlService = new TestCqlService("http://localhost", {
example, // Fix types
});
const cqlInput = createCqlInput(testCqlService);
customElements.define("cql-input", cqlInput);

const mountComponent = () => {
document.body.innerHTML = `<cql-input></cql-input>`;
const container = document.createElement("div");
container.innerHTML = `<cql-input data-testid="cql-input"></cql-input>`;
return container;
};

test("renders a custom element", () => {
mountComponent();
test("renders a custom element", async () => {
const container = mountComponent();
await findByTestId(container, "cql-input");
});
2 changes: 1 addition & 1 deletion prosemirror-client/src/cqlInput/CqlInput.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CqlService } from "../CqlService";
import { CqlServiceInterface } from "../services/CqlService";
import { QueryChangeEventDetail } from "./dom";
import { createEditor } from "./editor";

Expand Down
8 changes: 4 additions & 4 deletions prosemirror-client/src/cqlInput/TypeaheadPopover.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Mapping } from "prosemirror-transform";
import { TypeaheadSuggestion } from "../CqlService";
import { TypeaheadSuggestion } from "../services/CqlService";
import { findNodeAt } from "./utils";
import { EditorView } from "prosemirror-view";
import { chip, schema } from "./schema";
Expand Down Expand Up @@ -80,14 +80,14 @@ export class TypeaheadPopover {
this.updateItems(suggestions.TextSuggestion.suggestions);
}

this.popoverEl.showPopover();
this.popoverEl.showPopover?.();
} else {
this.currentSuggestion = undefined;
this.popoverEl.hidePopover();
this.popoverEl.hidePopover?.();
}
} else {
this.currentSuggestion = undefined;
this.popoverEl.hidePopover();
this.popoverEl.hidePopover?.();
this.popoverEl.innerHTML = "";
}
};
Expand Down
4 changes: 2 additions & 2 deletions prosemirror-client/src/cqlInput/editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EditorView } from "prosemirror-view";
import { CqlService } from "../CqlService";
import { CqlServiceInterface } from "../services/CqlService";
import { createCqlPlugin } from "./plugin";
import { EditorState } from "prosemirror-state";
import { doc, schema, searchText } from "./schema";
Expand All @@ -26,7 +26,7 @@ export const createEditor = ({
}: {
mountEl: HTMLElement;
popoverEl: HTMLElement;
cqlService: CqlService;
cqlService: CqlServiceInterface;
onChange: (detail: QueryChangeEventDetail) => void;
debugEl?: HTMLElement;
}) => {
Expand Down
36 changes: 36 additions & 0 deletions prosemirror-client/src/cqlInput/fixtures/responses/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"tokens": [
{
"type": "Token",
"tokenType": "STRING",
"lexeme": "example",
"start": 0,
"end": 6,
"literal": "example"
},
{
"type": "Token",
"tokenType": "EOF",
"lexeme": "",
"start": 7,
"end": 7,
"literal": null
}
],
"ast": {
"type": "QueryList",
"content": [
{
"type": "QueryBinary",
"left": {
"type": "QueryContent",
"searchExpr": "example"
},
"right": null
}
]
},
"queryResult": "q=example",
"suggestions": [],
"error": null
}
7 changes: 5 additions & 2 deletions prosemirror-client/src/cqlInput/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DecorationSet } from "prosemirror-view";
import { CqlService, TypeaheadSuggestion } from "../CqlService";
import {
CqlServiceInterface,
TypeaheadSuggestion,
} from "../services/CqlService";
import {
AllSelection,
Plugin,
Expand Down Expand Up @@ -44,7 +47,7 @@ export const createCqlPlugin = ({
onChange,
debugEl,
}: {
cqlService: CqlService;
cqlService: CqlServiceInterface;
popoverEl: HTMLElement;
onChange: (detail: QueryChangeEventDetail) => void;
debugEl?: HTMLElement;
Expand Down
10 changes: 5 additions & 5 deletions prosemirror-client/src/cqlInput/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, test } from "bun:test";
import _tokensWithOneKVPair from "./fixtures/tokensWithOneKVPair.json";
import _tokensWithTwoKVPairs from "./fixtures/tokensWithTwoKVPairs.json";
import _tokensWithParens from "./fixtures/tokensWithParens.json";
import _tokensWithParensAndKVPair from "./fixtures/tokensWithParensAndKVPair.json";
import _tokensWithTrailingWhitespace from "./fixtures/tokensWithTrailingWhitespace.json";
import _tokensWithOneKVPair from "./fixtures/tokens/tokensWithOneKVPair.json";
import _tokensWithTwoKVPairs from "./fixtures/tokens/tokensWithTwoKVPairs.json";
import _tokensWithParens from "./fixtures/tokens/tokensWithParens.json";
import _tokensWithParensAndKVPair from "./fixtures/tokens/tokensWithParensAndKVPair.json";
import _tokensWithTrailingWhitespace from "./fixtures/tokens/tokensWithTrailingWhitespace.json";
import {
mapTokens,
toProseMirrorTokens,
Expand Down
2 changes: 1 addition & 1 deletion prosemirror-client/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createCqlInput } from "./cqlInput/CqlInput";
import applyDevTools from "prosemirror-dev-tools";
import "./style.css";
import { CqlService } from "./CqlService";
import { CqlService } from "./services/CqlService";

const debugEl = document.createElement("div");
debugEl.className = "CqlSandbox__debug-container";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ type TextSuggestion = { suggestions: Array<TextSuggestionOption> };
type TextSuggestionOption = { label: string; value: string };
type DateSuggestion = { validFrom?: string; validTo?: string };

export class CqlService {
export interface CqlServiceInterface {
setUrl(url: string): void;

fetchResult(query: string): Promise<CqlResult>;

cancel(): void;
}

export class CqlService implements CqlServiceInterface {
private abortController: AbortController | undefined;

constructor(private url: string) {}
Expand Down
34 changes: 34 additions & 0 deletions prosemirror-client/src/services/TestCqlService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CqlResult, CqlServiceInterface } from "./CqlService";

export class TestCqlService implements CqlServiceInterface {
private abortController: AbortController | undefined;

/**
* @param url
* @param resultFixtures A map from query strings to results
*/
constructor(
private url: string,
private resultFixtures: Record<string, CqlResult>
) {}

public setUrl(url: string) {
this.url = url;
}

public async fetchResult(query: string) {
const result = this.resultFixtures[query];

if (result) {
return Promise.resolve(result);
}

return Promise.reject(
new Error(`Fixture not found for query string ${query}`)
);
}

public cancel() {
this.abortController?.abort();
}
}

0 comments on commit cfb5d04

Please sign in to comment.