Skip to content

Commit

Permalink
test(repository): fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed May 12, 2020
1 parent 9488fa4 commit 64673d5
Showing 1 changed file with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('HasManyThroughHelpers', () => {
type: RelationType.hasOne,
targetsMany: false,
source: Category,
target: () => Category,
target: () => Product,
};

expect(() => {
Expand All @@ -66,14 +66,30 @@ describe('HasManyThroughHelpers', () => {
);
});

it('throws if the through is not provided', async () => {
const metadata: unknown = {
name: 'category',
type: RelationType.hasMany,
targetsMany: true,
source: Category,
target: () => Product,
};

expect(() => {
resolveHasManyThroughMetadata(metadata as HasManyDefinition);
}).to.throw(
/Invalid hasMany definition for Category#category: through must be specified/,
);
});

it('throws if the through is not resolvable', async () => {
const metadata: unknown = {
name: 'category',
type: RelationType.hasMany,
targetsMany: true,
source: Category,
through: {model: 'random'},
target: () => Category,
target: () => Product,
};

expect(() => {
Expand Down Expand Up @@ -154,7 +170,7 @@ describe('HasManyThroughHelpers', () => {
expect(meta).to.eql(resolvedMetadata);
});

it('throws if through.keyFrom, through.keyTo, and default foreign key name are not provided in through', async () => {
it('throws if through.keyFrom is not provided in through', async () => {
const metadata = {
name: 'categories',
type: RelationType.hasMany,
Expand All @@ -176,6 +192,53 @@ describe('HasManyThroughHelpers', () => {
/Invalid hasMany definition for Category#categories: through model InvalidThrough is missing definition of source foreign key/,
);
});

it('throws if through.keyTo is not provided in through', async () => {
const metadata = {
name: 'categories',
type: RelationType.hasMany,
targetsMany: true,
source: Category,
keyFrom: 'id',
target: () => Product,
keyTo: 'id',

through: {
model: () => InvalidThrough2,
keyFrom: 'categoryId',
},
};

expect(() => {
resolveHasManyThroughMetadata(metadata as HasManyDefinition);
}).to.throw(
/Invalid hasMany definition for Category#categories: through model InvalidThrough2 is missing definition of target foreign key/,
);
});

it('throws if the tarhet model does not have the id property', async () => {
const metadata = {
name: 'categories',
type: RelationType.hasMany,
targetsMany: true,
source: Category,
keyFrom: 'id',
target: () => InvalidProduct,
keyTo: 'id',

through: {
model: () => CategoryProductLink,
keyFrom: 'categoryId',
keyTo: 'productId',
},
};

expect(() => {
resolveHasManyThroughMetadata(metadata as HasManyDefinition);
}).to.throw(
'Invalid hasMany definition for Category#categories: target model InvalidProduct does not have any primary key (id property)',
);
});
});
});
/****** HELPERS *******/
Expand All @@ -200,6 +263,15 @@ describe('HasManyThroughHelpers', () => {
}
}

@model()
class InvalidProduct extends Entity {
@property({id: false})
random: number;
constructor(data: Partial<InvalidProduct>) {
super(data);
}
}

@model()
class CategoryProductLink extends Entity {
@property({id: true})
Expand Down Expand Up @@ -239,6 +311,16 @@ describe('HasManyThroughHelpers', () => {
// lack through.keyFrom
.addProperty('productId', {type: 'number'});

class InvalidThrough2 extends Entity {}
InvalidThrough2.definition = new ModelDefinition('InvalidThrough2')
.addProperty('id', {
type: 'number',
id: true,
required: true,
})
// lack through.keyTo
.addProperty('categoryId', {type: 'number'});

function createCategoryProductLink(properties: Partial<CategoryProductLink>) {
return new CategoryProductLink(properties);
}
Expand Down

0 comments on commit 64673d5

Please sign in to comment.