- add more inheritance tests - add support for custom discriminator properties - add ".idea" to gitignore - prop: add support for extended array items this is a squash merge of typegoose/typegoose#21pull/49/head
parent
b13c5f8a20
commit
35a9681e41
@ -0,0 +1,41 @@ |
||||
import { arrayProp, prop } from '../../src/prop'; |
||||
import { getModelForClass, modelOptions } from '../../src/typegoose'; |
||||
|
||||
@modelOptions({ |
||||
schemaOptions: { |
||||
discriminatorKey: 'width', |
||||
collection: 'buildings' |
||||
} |
||||
}) |
||||
export class Building { |
||||
@prop({default: 100}) |
||||
public width: number; |
||||
} |
||||
|
||||
export class OfficeBuilding extends Building { |
||||
@prop({default: 4}) |
||||
public doors: number; |
||||
} |
||||
|
||||
export class Garage extends Building { |
||||
@prop({default: 10}) |
||||
public slotsForCars: number; |
||||
} |
||||
|
||||
@modelOptions({ |
||||
schemaOptions: { |
||||
collection: 'skyscrapers' |
||||
} |
||||
}) |
||||
export class Skyscraper extends OfficeBuilding { |
||||
@prop({default: 'Some cool string'}) |
||||
public name: string; |
||||
|
||||
@prop() |
||||
public mainGarage: Garage; |
||||
|
||||
@arrayProp({items: Garage}) |
||||
public garagesInArea: Garage[]; |
||||
} |
||||
|
||||
export const model = getModelForClass<Skyscraper, any >(Skyscraper); |
@ -0,0 +1,43 @@ |
||||
import { expect } from 'chai'; |
||||
import { model as inheritanceClass, Skyscraper } from '../models/inheritanceClass'; |
||||
|
||||
export function suite() { |
||||
it('should set all direct parent props', async () => { |
||||
const instance = await inheritanceClass.create({}); |
||||
expect(instance.name).to.equals('Some cool string'); |
||||
expect(instance.doors).to.equals(4); |
||||
expect(instance.width).to.equals(100); |
||||
}); |
||||
|
||||
it('should merge all parent schema options', async () => { |
||||
const instance = await inheritanceClass.create({}); |
||||
expect(instance.schema.get('collection')).to.equals('skyscrapers'); |
||||
expect(instance.schema.get('discriminatorKey')).to.equals('width'); |
||||
}); |
||||
|
||||
it('should set all parent props for nested schemas', async () => { |
||||
const input = { |
||||
mainGarage: { |
||||
slotsForCars: 3 |
||||
} |
||||
} as Skyscraper; |
||||
const instance = await inheritanceClass.create(input); |
||||
expect(instance.mainGarage.slotsForCars).to.equals(3); |
||||
expect(instance.mainGarage.width).to.equals(100); |
||||
expect(instance.mainGarage.doors).to.equals(undefined); |
||||
}); |
||||
|
||||
it('should set all parent props for nested array items', async () => { |
||||
const input = { |
||||
garagesInArea: [{ |
||||
slotsForCars: 2 |
||||
}] |
||||
} as Skyscraper; |
||||
const instance = await inheritanceClass.create(input); |
||||
expect(instance.garagesInArea).to.be.lengthOf(1); |
||||
const firstGarage = instance.garagesInArea.pop(); |
||||
expect(firstGarage.slotsForCars).to.equals(2); |
||||
expect(firstGarage.width).to.equals(100); |
||||
expect(firstGarage.doors).to.be.equals(undefined); |
||||
}); |
||||
} |
Loading…
Reference in new issue