Skip to content

Commit 20f50b7

Browse files
authored
[feat-2] Setup factory and database (#3)
1 parent c28bd5e commit 20f50b7

File tree

9 files changed

+968
-12
lines changed

9 files changed

+968
-12
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module.exports = {
44
plugins: ['@typescript-eslint'],
55
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
66
rules: {
7-
'@typescript-eslint/explicit-module-boundary-types': 'off'
7+
'@typescript-eslint/explicit-module-boundary-types': 'off',
8+
'@typescript-eslint/no-explicit-any': 'off',
9+
'prefer-const': 'off'
810
}
911
};

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
dist/
2+
dist/
3+
.vscode/

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "typical-data",
3-
"version": "0.0.2",
4-
"description": "Test data factory written in TypeScript",
3+
"version": "0.1.0",
4+
"description": "Test data factory",
55
"homepage": "https://github.com/davidtkramer/typical-data",
66
"main": "./dist/index.js",
77
"types": "./dist/index.d.ts",

src/__tests__/database.test.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { Database } from '../database';
2+
import { Factory } from '../factory';
3+
4+
interface User {
5+
id: number;
6+
name: string;
7+
isAdmin: boolean;
8+
}
9+
10+
const userFactory = Factory.define((factory) =>
11+
factory
12+
.transient({
13+
foo: 'foo',
14+
})
15+
.attributes<User>({
16+
id: ({ sequence }) => sequence + 1,
17+
name: 'Alice',
18+
isAdmin: false,
19+
})
20+
.trait('admin', (trait) =>
21+
trait.attributes({
22+
isAdmin: true,
23+
})
24+
)
25+
);
26+
27+
describe('Database factory', () => {
28+
describe('create', () => {
29+
it('loads unnamed fixtures on creation', () => {
30+
const db = Database.create({
31+
models: { users: userFactory },
32+
fixtures({ users }) {
33+
users.create();
34+
},
35+
});
36+
37+
expect(db.users).toHaveLength(1);
38+
});
39+
40+
it('loads named fixtures on creation', () => {
41+
const db = Database.create({
42+
models: { users: userFactory },
43+
fixtures({ users }) {
44+
return {
45+
users: { current: users.create() },
46+
};
47+
},
48+
});
49+
50+
expect(db.users).toHaveLength(1);
51+
expect(db.fixtures.users.current).toBeDefined();
52+
});
53+
});
54+
});
55+
56+
describe('database instance', () => {
57+
describe('reset', () => {
58+
it('resets entities and fixtures', () => {
59+
const db = Database.create({
60+
models: { users: userFactory },
61+
fixtures({ users }) {
62+
return {
63+
users: { current: users.create() },
64+
};
65+
},
66+
});
67+
68+
expect(db.users).toHaveLength(1);
69+
expect(db.fixtures.users.current).toBeDefined();
70+
const user = db.fixtures.users.current;
71+
db.reset();
72+
expect(db.users).toHaveLength(1);
73+
expect(db.fixtures.users.current).toBeDefined();
74+
expect(db.fixtures.users.current).not.toBe(user);
75+
});
76+
});
77+
});
78+
79+
describe('entity store', () => {
80+
const db = Database.create({
81+
models: {
82+
users: userFactory,
83+
},
84+
});
85+
86+
beforeEach(() => {
87+
db.reset();
88+
});
89+
90+
describe('create', () => {
91+
it('creates default entity', () => {
92+
const user = db.users.create();
93+
expect(user).toEqual({
94+
id: 1,
95+
isAdmin: false,
96+
name: 'Alice',
97+
});
98+
});
99+
100+
it('creates entity with trait and attribute overrides', () => {
101+
const user = db.users.create('admin', { name: 'Bob', foo: 'bar' });
102+
expect(user).toEqual({
103+
id: 1,
104+
name: 'Bob',
105+
isAdmin: true,
106+
});
107+
});
108+
});
109+
110+
describe('createList', () => {
111+
it('creates list of default entities', () => {
112+
const users = db.users.createList(2);
113+
expect(users).toHaveLength(2);
114+
expect(users[0]).toEqual({ id: 1, name: 'Alice', isAdmin: false });
115+
expect(users[1]).toEqual({ id: 2, name: 'Alice', isAdmin: false });
116+
});
117+
118+
it('create list of entities with trait and attribute overrides', () => {
119+
const users = db.users.createList(2, 'admin', {
120+
name: 'Bob',
121+
foo: 'bar',
122+
});
123+
expect(users).toHaveLength(2);
124+
expect(users[0].name).toBe('Bob');
125+
expect(users[0].isAdmin).toBe(true);
126+
expect(users[1].name).toBe('Bob');
127+
expect(users[1].isAdmin).toBe(true);
128+
});
129+
});
130+
131+
describe('reset', () => {
132+
it('resets entity store', () => {
133+
db.users.createList(2);
134+
expect(db.users).toHaveLength(2);
135+
const deletedUsers = db.users.reset();
136+
expect(deletedUsers).toHaveLength(2);
137+
expect(db.users).toHaveLength(0);
138+
});
139+
});
140+
});

0 commit comments

Comments
 (0)