Skip to content

Commit 6c8f611

Browse files
authored
Fix types for fixtures map (#37)
1 parent 83be10f commit 6c8f611

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
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.0",
3+
"version": "0.4.1",
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: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ describe('Database factory', () => {
5252
expect(db.fixtures.users.current).toBeDefined();
5353
});
5454

55+
it('does not require named fixtures for every entity type', () => {
56+
const db = createDatabase({
57+
factories: {
58+
users: createFactory<{ id: string }>({ id: '1' }),
59+
contacts: createFactory<{ id: string }>({ id: '1' }),
60+
},
61+
fixtures({ users }) {
62+
return {
63+
// okay to not provide fixtures for contacts
64+
users: { current: users.create() },
65+
};
66+
},
67+
});
68+
69+
expect(db.fixtures.users.current).toBeDefined();
70+
});
71+
5572
it('handles nested factories', () => {
5673
type BaseContact = { id: number; type: 'business' | 'individual' };
5774
interface IndividualContact extends BaseContact {
@@ -82,26 +99,40 @@ describe('Database factory', () => {
8299
),
83100
},
84101
},
102+
fixtures(self) {
103+
return {
104+
contacts: {
105+
contact1: self.contacts.business.create(),
106+
contact2: self.contacts.individual.create(),
107+
},
108+
};
109+
},
85110
});
86111

87112
db.contacts.individual.create();
88113
db.contacts.individual.createList(1);
89114
db.contacts.business.create();
90115
db.contacts.business.createList(1);
91116

92-
expect(db.contacts).toHaveLength(4);
117+
expect(db.contacts).toHaveLength(6);
118+
expect(db.fixtures.contacts.contact1).toBeDefined();
119+
expect(db.fixtures.contacts.contact2).toBeDefined();
93120

94121
// can store mix of entity types
95-
expect(db.contacts[0].type).toBe('individual');
122+
expect(db.contacts[0].type).toBe('business');
96123
expect(db.contacts[1].type).toBe('individual');
97-
expect(db.contacts[2].type).toBe('business');
98-
expect(db.contacts[3].type).toBe('business');
124+
expect(db.contacts[2].type).toBe('individual');
125+
expect(db.contacts[3].type).toBe('individual');
126+
expect(db.contacts[4].type).toBe('business');
127+
expect(db.contacts[5].type).toBe('business');
99128

100129
// shares sequence between entity types
101130
expect(db.contacts[0].id).toBe(0);
102131
expect(db.contacts[1].id).toBe(1);
103132
expect(db.contacts[2].id).toBe(2);
104133
expect(db.contacts[3].id).toBe(3);
134+
expect(db.contacts[4].id).toBe(4);
135+
expect(db.contacts[5].id).toBe(5);
105136

106137
db.contacts.reset();
107138

src/database.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ type EntityFromFactory<Factory> = Factory extends EntityFactory<
99
: never;
1010

1111
type FixtureMap<FC extends FactoryConfig> = {
12-
[Property in keyof FC]: Record<string, EntityFromFactory<FC[Property]>>;
12+
[Property in keyof FC]?: FC[Property] extends FactoryMap
13+
? Record<string, EntityFromFactory<FC[Property][keyof FC[Property]]>>
14+
: Record<string, EntityFromFactory<FC[Property]>>;
1315
};
1416

1517
interface FactoryMap {

0 commit comments

Comments
 (0)