Skip to content

Commit

Permalink
test: add playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Apr 2, 2024
1 parent 7180141 commit 509b9f6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
bun-version: 1.1.0
- name: Install dependencies, build, and test
run: bun install
- run: bunx playwright install
- run: bun run build
- run: bun run bundlewatch
- run: bun run test
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default function diff(
oldNode: Node,
reader: ReadableStreamDefaultReader<Uint8Array>,
reader: ReadableStreamDefaultReader<unknown>,
): Promise<void>;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"devDependencies": {
"typescript": "5.4.3",
"@types/bun": "1.0.12",
"bundlewatch": "0.3.3"
"bundlewatch": "0.3.3",
"playwright": "1.42.1"
}
}
48 changes: 48 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from 'bun:test';
import { chromium, type Browser, type Page } from 'playwright';
import diff from './index';
import {join} from 'node:path';

const transpiler = new Bun.Transpiler({ loader: 'ts', target: 'browser' });
const content = await transpiler.transform((await Bun.file(join(import.meta.dir, 'index.ts')).text()).replace('export default', ''));

describe('Diffing Algorithm Test', () => {
let browser: Browser;
let page: Page;

beforeAll(async () => {
browser = await chromium.launch();
});

beforeEach(async () => {
page = await browser.newPage();
})

afterEach(async () => {
await page.close();
});

afterAll(async () => {
await browser.close();
});

it('should update the DOM correctly', async () => {
await page.setContent('<div id="test">Old Node</div>');
await page.evaluate(async ([content]) => {
eval(content)
const encoder = new TextEncoder();
const readable = new ReadableStream({ start: (controller) => {
controller.enqueue(encoder.encode('<div id="test">'));
controller.enqueue(encoder.encode('New Node Content'));
controller.enqueue(encoder.encode("</div>"));
controller.close();
}});
const reader = readable.getReader();

await diff(document.getElementById('test')!, reader)
}, [content]);

const newNode = await page.$eval('#test', (node: Node) => node.textContent!.trim());
expect(newNode).toBe('New Node Content');
});
});
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const decoder = new TextDecoder();

export default async function diff(
oldNode: Node,
reader: ReadableStreamDefaultReader<Uint8Array>,
reader: ReadableStreamDefaultReader<unknown>,
) {
const walker = await htmlStreamWalker(reader);
const newNode = walker.rootNode!;
Expand Down Expand Up @@ -175,7 +175,7 @@ function getKey(node: Node) {
}

async function htmlStreamWalker(
streamReader: ReadableStreamDefaultReader<Uint8Array>,
streamReader: ReadableStreamDefaultReader<unknown>,
): Promise<Walker> {
const doc = document.implementation.createHTMLDocument();
let closed = false;
Expand Down

0 comments on commit 509b9f6

Please sign in to comment.