Skip to content

Commit f511059

Browse files
committed
test(discriminators): add tests for DiscriminatorTypeComposer methods
1 parent 73afb58 commit f511059

File tree

3 files changed

+408
-96
lines changed

3 files changed

+408
-96
lines changed

src/__tests__/composeWithMongooseDiscriminators-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { DiscriminatorTypeComposer } from '../discriminators';
99
beforeAll(() => MovieModel.base.connect());
1010
afterAll(() => MovieModel.base.disconnect());
1111

12-
const { CharacterModel, PersonModel, DroidModel } = getCharacterModels('type');
12+
const { CharacterModel, PersonModel } = getCharacterModels('type');
1313

1414
describe('composeWithMongooseDiscriminators ->', () => {
1515
beforeEach(() => {

src/discriminators/DiscriminatorTypeComposer.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,13 @@ export class DiscriminatorTypeComposer<TContext> extends TypeComposerClass<TCont
174174
}
175175

176176
setFields(fields: ComposeFieldConfigMap<any, any>): DiscriminatorTypeComposer<TContext> {
177+
const oldFieldNames = super.getFieldNames();
177178
super.setFields(fields);
178179

179180
for (const childTC of this.childTCs) {
180-
childTC.setFields(fields);
181+
childTC.removeField(oldFieldNames);
182+
childTC.addFields(fields);
183+
reorderFields(childTC, (this.opts: any).reorderFields, this.getDKey(), super.getFieldNames());
181184
}
182185

183186
return this;
@@ -228,10 +231,18 @@ export class DiscriminatorTypeComposer<TContext> extends TypeComposerClass<TCont
228231
}
229232

230233
removeOtherFields(fieldNameOrArray: string | Array<string>): DiscriminatorTypeComposer<TContext> {
234+
const oldFieldNames = super.getFieldNames();
231235
super.removeOtherFields(fieldNameOrArray);
232236

233237
for (const childTC of this.childTCs) {
234-
childTC.removeOtherFields(fieldNameOrArray);
238+
const specificFields = childTC
239+
.getFieldNames()
240+
.filter(
241+
childFieldName =>
242+
!oldFieldNames.find(oldBaseFieldName => oldBaseFieldName === childFieldName)
243+
);
244+
childTC.removeOtherFields(super.getFieldNames().concat(specificFields));
245+
reorderFields(childTC, (this.opts: any).reorderFields, this.getDKey(), super.getFieldNames());
235246
}
236247

237248
return this;

0 commit comments

Comments
 (0)