Skip to content

Commit

Permalink
Add some very basic bun tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann Jacobsohn committed Oct 16, 2023
1 parent 28c00ed commit a1e83f6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = "./happydom.ts"
2 changes: 2 additions & 0 deletions happydom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { GlobalRegistrator } from "@happy-dom/global-registrator";
GlobalRegistrator.register();
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"compile:watch": "tsex compile --watch",
"demo": "vite demo",
"test": "tsex test",
"bun:test": "bun test",
"test:watch": "tsex test --watch",
"prepublishOnly": "tsex prepare"
},
Expand All @@ -23,7 +24,11 @@
"library"
],
"devDependencies": {
"@happy-dom/global-registrator": "^12.9.1",
"bun": "^1.0.5",
"bun-types": "^1.0.5-canary.20231009T140142",
"fava": "^0.2.1",
"happy-dom": "^12.9.1",
"tsex": "^3.0.1",
"typescript": "^5.1.6",
"vite": "^4.4.9"
Expand Down
68 changes: 68 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/// <reference lib="dom" />

import {test, expect} from 'bun:test';
import ShoSho from '../dist/index.js';

const keys = {
f: {'key': 'f'},
g: {'key': 'g'},
h: {'key': 'h'},
}

function keypress(key){
document.dispatchEvent(new KeyboardEvent('keydown', keys[key]));
document.dispatchEvent(new KeyboardEvent('keyup', keys[key]));
}

test ( 'key sequences do not trigger single key shortcuts', async () => {
let f_pressed = 0;
let g_pressed = 0;
let fg_pressed = 0;

const sho = new ShoSho ();
sho.register('f', () => { f_pressed++; });
sho.register('g', () => { g_pressed++; });
sho.register('f g', () => { fg_pressed++; return true });
sho.start()

keypress('f')
expect(f_pressed).toEqual(1)

await new Promise(resolve => setTimeout(() => resolve(''), 1000));

keypress('g')
expect(g_pressed).toEqual(1)
expect(fg_pressed).toEqual(0)

keypress('f')
keypress('g')
expect(f_pressed).toEqual(1)
expect(g_pressed).toEqual(1)
expect(fg_pressed).toEqual(1)
})

test ( 'key sequences are not triggered by single keys', () => {
let fg_pressed = 0;
let fh_pressed = 0;

const sho = new ShoSho ();

sho.register('f g', () => { fg_pressed++; return true });
sho.register('f h', () => { fh_pressed++; return true });
sho.start()

keypress('f')
keypress('g')
expect(fg_pressed).toEqual(1)
expect(fh_pressed).toEqual(0)

keypress('f')
keypress('h')
expect(fg_pressed).toEqual(1)
expect(fh_pressed).toEqual(1)

keypress('g')
keypress('h')
expect(fg_pressed).toEqual(1)
expect(fh_pressed).toEqual(1)
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "tsex/tsconfig.json",
"compilerOptions": {
"lib": ["esnext", "dom"],
"noImplicitAny": false
}
}

0 comments on commit a1e83f6

Please sign in to comment.