@@ -52,6 +52,23 @@ describe('Database factory', () => {
52
52
expect ( db . fixtures . users . current ) . toBeDefined ( ) ;
53
53
} ) ;
54
54
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
+
55
72
it ( 'handles nested factories' , ( ) => {
56
73
type BaseContact = { id : number ; type : 'business' | 'individual' } ;
57
74
interface IndividualContact extends BaseContact {
@@ -82,26 +99,40 @@ describe('Database factory', () => {
82
99
) ,
83
100
} ,
84
101
} ,
102
+ fixtures ( self ) {
103
+ return {
104
+ contacts : {
105
+ contact1 : self . contacts . business . create ( ) ,
106
+ contact2 : self . contacts . individual . create ( ) ,
107
+ } ,
108
+ } ;
109
+ } ,
85
110
} ) ;
86
111
87
112
db . contacts . individual . create ( ) ;
88
113
db . contacts . individual . createList ( 1 ) ;
89
114
db . contacts . business . create ( ) ;
90
115
db . contacts . business . createList ( 1 ) ;
91
116
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 ( ) ;
93
120
94
121
// can store mix of entity types
95
- expect ( db . contacts [ 0 ] . type ) . toBe ( 'individual ' ) ;
122
+ expect ( db . contacts [ 0 ] . type ) . toBe ( 'business ' ) ;
96
123
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' ) ;
99
128
100
129
// shares sequence between entity types
101
130
expect ( db . contacts [ 0 ] . id ) . toBe ( 0 ) ;
102
131
expect ( db . contacts [ 1 ] . id ) . toBe ( 1 ) ;
103
132
expect ( db . contacts [ 2 ] . id ) . toBe ( 2 ) ;
104
133
expect ( db . contacts [ 3 ] . id ) . toBe ( 3 ) ;
134
+ expect ( db . contacts [ 4 ] . id ) . toBe ( 4 ) ;
135
+ expect ( db . contacts [ 5 ] . id ) . toBe ( 5 ) ;
105
136
106
137
db . contacts . reset ( ) ;
107
138
0 commit comments