Skip to content

Commit

Permalink
add tests for tree service (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
Opeyem1a authored Oct 14, 2024
1 parent 5600abc commit 4d4d42a
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/services/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';

type JSONPrimitive = string | number | boolean | null | undefined;

type JSONValue =
export type JSONValue =
| JSONPrimitive
| JSONValue[]
| {
Expand Down Expand Up @@ -42,9 +42,11 @@ export interface StoreService {
write: (data: JSONValue) => void;
read: () => JSONValue;
}

export interface ServiceConfig {
filename: string;
}

export const createStoreService = (config: ServiceConfig): StoreService => {
return {
write: (data: JSONValue) => {
Expand Down
164 changes: 164 additions & 0 deletions src/services/tree.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { JSONValue } from './store.js';
import { createTreeService } from './tree.js';
import { describe, expect, it, vi } from 'vitest';

vi.mock('./store.js', () => {
let fileData = '';
return {
createStoreService: vi.fn(({}) => {
return {
read: (): JSONValue => {
return JSON.parse(fileData) as JSONValue;
},
write: (data: JSONValue) => {
fileData = JSON.stringify(data);
},
};
}),
};
});

describe('tree service is working', () => {
it('registers root branch', () => {
const { registerRoot, get } = createTreeService();
registerRoot('root');
expect(get()).to.deep.equal([{ key: 'root', parent: null }]);
});

it('overwrites past roots with the new root branch', () => {
const { registerRoot, attachTo, get } = createTreeService();
registerRoot('root');
registerRoot('root_2');
registerRoot('root_3');
expect(get()).to.deep.equal([{ key: 'root_3', parent: null }]);

registerRoot('root');
attachTo({ newBranch: 'some-new-branch', parent: 'root' });
attachTo({ newBranch: 'some-new-branch-2', parent: 'some-new-branch' });
registerRoot('root_new');
expect(get()).to.deep.equal([{ key: 'root_new', parent: null }]);
});

it('can attach a branch to a parent', () => {
const { registerRoot, attachTo, get } = createTreeService();
registerRoot('root');
attachTo({ newBranch: 'some-new-branch', parent: 'root' });
expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'some-new-branch', parent: 'root' },
]);
});

it('can move a branch to a parent', () => {
const { registerRoot, attachTo, moveOnto, get } = createTreeService();
registerRoot('root');
attachTo({ newBranch: 'branch_a', parent: 'root' });
attachTo({ newBranch: 'branch_b', parent: 'root' });
attachTo({ newBranch: 'branch_a_a', parent: 'branch_a' });

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b', parent: 'root' },
{ key: 'branch_a_a', parent: 'branch_a' },
]);

moveOnto({ branch: 'branch_a_a', parent: 'root' });

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b', parent: 'root' },
{ key: 'branch_a_a', parent: 'root' },
]);

moveOnto({ branch: 'branch_b', parent: 'branch_a' });

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b', parent: 'branch_a' },
{ key: 'branch_a_a', parent: 'root' },
]);
});

it('can remove branches', () => {
const { registerRoot, attachTo, removeBranch, get } =
createTreeService();

registerRoot('root');
attachTo({ newBranch: 'branch_a', parent: 'root' });
attachTo({ newBranch: 'branch_b', parent: 'root' });
attachTo({ newBranch: 'branch_a_a', parent: 'branch_a' });
attachTo({ newBranch: 'branch_b_a', parent: 'branch_b' });
attachTo({ newBranch: 'branch_b_b', parent: 'branch_b' });

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b', parent: 'root' },
{ key: 'branch_a_a', parent: 'branch_a' },
{ key: 'branch_b_a', parent: 'branch_b' },
{ key: 'branch_b_b', parent: 'branch_b' },
]);

removeBranch('branch_a_a');

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b', parent: 'root' },
{ key: 'branch_b_a', parent: 'branch_b' },
{ key: 'branch_b_b', parent: 'branch_b' },
]);

removeBranch('branch_b');

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b_a', parent: 'branch_b' },
{ key: 'branch_b_b', parent: 'branch_b' },
]);
});

it('can remove parent branches without removing the child branches', () => {
const { registerRoot, attachTo, removeBranch, get } =
createTreeService();

registerRoot('root');
attachTo({ newBranch: 'branch_a', parent: 'root' });
attachTo({ newBranch: 'branch_b', parent: 'root' });
attachTo({ newBranch: 'branch_a_a', parent: 'branch_a' });
attachTo({ newBranch: 'branch_b_a', parent: 'branch_b' });
attachTo({ newBranch: 'branch_b_b', parent: 'branch_b' });

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a', parent: 'root' },
{ key: 'branch_b', parent: 'root' },
{ key: 'branch_a_a', parent: 'branch_a' },
{ key: 'branch_b_a', parent: 'branch_b' },
{ key: 'branch_b_b', parent: 'branch_b' },
]);

removeBranch('branch_a');

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_b', parent: 'root' },
{ key: 'branch_a_a', parent: 'branch_a' },
{ key: 'branch_b_a', parent: 'branch_b' },
{ key: 'branch_b_b', parent: 'branch_b' },
]);

removeBranch('branch_b');

expect(get()).to.deep.equal([
{ key: 'root', parent: null },
{ key: 'branch_a_a', parent: 'branch_a' },
{ key: 'branch_b_a', parent: 'branch_b' },
{ key: 'branch_b_b', parent: 'branch_b' },
]);
});
});
1 change: 1 addition & 0 deletions src/services/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const moveOnto = (
},
deps: { storeService: StoreService }
) => {
// todo: validate they cant be the same branch
const tree = _readTree(deps);

const parentBranch = _findParent({ parent, tree });
Expand Down

0 comments on commit 4d4d42a

Please sign in to comment.