Skip to content

Commit 7751eff

Browse files
authored
Add 'all' method to entity store (#40)
1 parent b804988 commit 7751eff

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typical-data",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "Test data factory",
55
"homepage": "https://github.com/davidtkramer/typical-data",
66
"main": "./dist/index.js",

src/__tests__/database.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,40 @@ describe('entity store', () => {
219219
});
220220
});
221221

222+
describe('all', () => {
223+
it('returns all entities for regular entity store', () => {
224+
db.users.createList(2);
225+
226+
const users = db.users.all();
227+
228+
expect(users).toHaveLength(2);
229+
// returns shallow copy of entities array
230+
db.users.create();
231+
expect(users).toHaveLength(2);
232+
});
233+
234+
it('returns all entities for polymorphic entity store', () => {
235+
const db = createDatabase({
236+
factories: {
237+
contacts: {
238+
individual: createFactory({ id: 1, fullName: '' }),
239+
business: createFactory({ id: 1, businessName: '' }),
240+
},
241+
},
242+
});
243+
244+
db.contacts.individual.create();
245+
db.contacts.business.create();
246+
247+
const contacts = db.contacts.all();
248+
249+
expect(contacts).toHaveLength(2);
250+
// returns shallow copy of entities array
251+
db.contacts.individual.create();
252+
expect(contacts).toHaveLength(2);
253+
});
254+
});
255+
222256
describe('reset', () => {
223257
it('resets entity store', () => {
224258
db.users.createList(2);

src/database.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ interface EntityStore<
2929
create: Factory['build'];
3030
createList: Factory['buildList'];
3131
reset(): Array<Entity>;
32+
all(): Array<Entity>;
3233
}
3334

3435
interface ParentEntityStore<
3536
Factory extends EntityFactory<unknown, unknown, unknown>,
3637
Entity = EntityFromFactory<Factory>
3738
> extends Array<Entity> {
3839
reset(): Array<Entity>;
40+
all(): Array<Entity>;
3941
}
4042

4143
type EntityStores<FC extends FactoryConfig> = {
@@ -103,6 +105,9 @@ export function createDatabase<
103105
factory.rewindSequence();
104106
return entities.splice(0, entities.length);
105107
},
108+
all() {
109+
return [...entities];
110+
},
106111
});
107112
database[key] = store;
108113
} else if (typeof item === 'object') {
@@ -113,6 +118,9 @@ export function createDatabase<
113118
sequence.count = -1;
114119
return entities.splice(0, entities.length);
115120
},
121+
all() {
122+
return [...entities];
123+
},
116124
});
117125

118126
for (const key in item) {

0 commit comments

Comments
 (0)