Skip to content

Commit

Permalink
add tests for form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
deebloo committed Feb 15, 2024
1 parent 4270260 commit 8efd1e7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
3 changes: 2 additions & 1 deletion dev.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
</style>

<form>
<go-board></go-board>
<button>submit</button>
<go-board name="board"></go-board>
</form>

<script src="./target/register.js" type="module"></script>
Expand Down
54 changes: 54 additions & 0 deletions src/board.element.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,58 @@ describe(GoBoardElement.name, () => {

expect(board.getSpace("C18")).to.be.null;
});

it("should submit the default key value to a form", async () => {
const board = await fixture<HTMLFormElement>(html`
<form>
<go-board name="game"></go-board>
<button>submit</button>
</form>
`);

return new Promise((resolve) => {
board.addEventListener("submit", (e) => {
e.preventDefault();

const form = new FormData(board);

expect(form.get("game")).to.equal(
"*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"
);

resolve();
});

board.querySelector("button")?.click();
});
});

it("should submit updated key when stones are added", async () => {
const board = await fixture<HTMLFormElement>(html`
<form>
<go-board name="game">
<go-stone slot="A1" color="black"></go-stone>
</go-board>
<button>submit</button>
</form>
`);

return new Promise((resolve) => {
board.addEventListener("submit", (e) => {
e.preventDefault();

const form = new FormData(board);

expect(form.get("game")).to.equal(
"******************************************************************************************************************************************************************************************************************************************************************************************************************************************************BA1******************"
);

resolve();
});

board.querySelector("button")?.click();
});
});
});
14 changes: 11 additions & 3 deletions src/board.element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,15 @@ export class GoBoardElement extends HTMLElement {
#spaces = new Map<string, GoStoneElement | null>();
#header = this.dom.query("#header")!;
#prevKey = "";
#currentKey = this.key();
#currentKey = Array.from({ length: this.rows * this.cols })
.map(() => "*")
.join("");

constructor() {
super();

this.#internals.setFormValue(this.#currentKey);
}

attributeChangedCallback(attr: string, old: string, val: string) {
if (this.debug) {
Expand Down Expand Up @@ -275,13 +283,13 @@ export class GoBoardElement extends HTMLElement {

for (let [space, stone] of this.#spaces) {
if (stone !== null) {
key += space + stone.color[0].toUpperCase();
key += stone.color[0].toUpperCase() + space;
} else {
key += "*";
}
}

return btoa(key);
return key;
}

reset() {
Expand Down

0 comments on commit 8efd1e7

Please sign in to comment.