Skip to content

Commit

Permalink
Add build and test workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Oct 5, 2024
1 parent 98b2d93 commit 3365f92
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Build and Test

on:
push:
branches: ['main']
pull_request:
branches: ['main']

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build
- run: npm test
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"test": "vitest",
"test": "vitest run",
"test:watch": "vitest",
"preview": "vite preview"
},
"devDependencies": {
Expand Down
40 changes: 20 additions & 20 deletions test/history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ import { describe, it, expect } from 'vitest';
import { History } from '../src/history';

type CounterCommand =
| { type: 'inc'; payload: { value: number } }
| { type: 'dec'; payload: { value: number } };
| { type: 'inc'; value: number }
| { type: 'dec'; value: number };

function execute(command: CounterCommand): CounterCommand {
if (command.type === 'inc') {
return { type: 'dec', payload: { value: command.payload.value } };
return { type: 'dec', value: command.value };
}

return { type: 'inc', payload: { value: command.payload.value } };
return { type: 'inc', value: command.value };
}

describe('history', () => {
it('should undo and redo commands', () => {
const history = new History<CounterCommand>(execute);

history.push({ type: 'inc', payload: { value: 1 } });
history.push({ type: 'inc', payload: { value: 2 } });
history.push({ type: 'inc', value: 1 });
history.push({ type: 'inc', value: 2 });

expect(history.undo()).toEqual({ type: 'dec', payload: { value: 2 } });
expect(history.undo()).toEqual({ type: 'dec', payload: { value: 1 } });
expect(history.undo()).toEqual({ type: 'dec', value: 2 });
expect(history.undo()).toEqual({ type: 'dec', value: 1 });
expect(history.undo()).toBeUndefined();

expect(history.redo()).toEqual({ type: 'inc', payload: { value: 1 } });
expect(history.redo()).toEqual({ type: 'inc', payload: { value: 2 } });
expect(history.redo()).toEqual({ type: 'inc', value: 1 });
expect(history.redo()).toEqual({ type: 'inc', value: 2 });
expect(history.redo()).toBeUndefined();
});

it('should serialize and deserialize history', () => {
const history = new History<CounterCommand>(execute);

history.push({ type: 'inc', payload: { value: 1 } });
history.push({ type: 'inc', payload: { value: 2 } });
history.push({ type: 'inc', value: 1 });
history.push({ type: 'inc', value: 2 });
expect(history.toJSON()).toEqual(
JSON.stringify({
undos: [
{ type: 'inc', payload: { value: 1 } },
{ type: 'inc', payload: { value: 2 } },
{ type: 'inc', value: 1 },
{ type: 'inc', value: 2 },
],
redos: [],
}),
Expand All @@ -47,8 +47,8 @@ describe('history', () => {
history.undo();
expect(history.toJSON()).toEqual(
JSON.stringify({
undos: [{ type: 'inc', payload: { value: 1 } }],
redos: [{ type: 'dec', payload: { value: 2 } }],
undos: [{ type: 'inc', value: 1 }],
redos: [{ type: 'dec', value: 2 }],
}),
);

Expand All @@ -57,17 +57,17 @@ describe('history', () => {
JSON.stringify({
undos: [],
redos: [
{ type: 'dec', payload: { value: 2 } },
{ type: 'dec', payload: { value: 1 } },
{ type: 'dec', value: 2 },
{ type: 'dec', value: 1 },
],
}),
);

history.redo();
expect(history.toJSON()).toEqual(
JSON.stringify({
undos: [{ type: 'inc', payload: { value: 1 } }],
redos: [{ type: 'dec', payload: { value: 2 } }],
undos: [{ type: 'inc', value: 1 }],
redos: [{ type: 'dec', value: 2 }],
}),
);
});
Expand Down
13 changes: 13 additions & 0 deletions test/view/selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,17 @@ describe('toRange', () => {
const result = toRange(range, container);
expect(result).toEqual({ s: 1, e: 9 });
});

it('should return error if the range is not in the container', () => {
const container = document.createElement('div');
container.innerHTML = 'Hello, World!';

const range = document.createRange();
range.setStart(container.firstChild!, 1);
range.setEnd(container.firstChild!, 4);

expect(() => toRange(range, document.createElement('div'))).toThrowError(
'node is not in the container',
);
});
});

0 comments on commit 3365f92

Please sign in to comment.