-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
find-by-foreign-keys.unit.ts
156 lines (139 loc) · 5.25 KB
/
find-by-foreign-keys.unit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
expect,
createStubInstance,
sinon,
StubbedInstanceWithSinonAccessor,
} from '@loopback/testlab';
import {findByForeignKeys} from '../../../..';
import {
ProductRepository,
Product,
createProduct,
} from './relations-helpers-fixtures';
describe('findByForeignKeys', () => {
let productRepo: StubbedInstanceWithSinonAccessor<ProductRepository>;
// use beforeEach to restore sinon stub
beforeEach(() => {
productRepo = createStubInstance(ProductRepository);
});
it('returns an empty array when no foreign keys are passed in', async () => {
const RESULTS: Product[] = [];
productRepo.stubs.find.resolves(RESULTS);
const fkIds: number[] = [];
await productRepo.create({id: 1, name: 'product', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', fkIds);
expect(products).to.be.empty();
sinon.assert.notCalled(productRepo.stubs.find);
});
it('returns an empty array when no instances have the foreign key value', async () => {
const find = productRepo.stubs.find;
find.resolves([]);
await productRepo.create({id: 1, name: 'product', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', 2);
expect(products).to.be.empty();
sinon.assert.calledWithMatch(find, {});
});
it('returns an empty array when no instances have the foreign key values', async () => {
const find = productRepo.stubs.find;
find.resolves([]);
await productRepo.create({id: 1, name: 'product', categoryId: 1});
const products = await findByForeignKeys(productRepo, 'categoryId', [2, 3]);
expect(products).to.be.empty();
sinon.assert.calledWithMatch(find, {});
});
it('returns all instances that have the foreign key value', async () => {
const find = productRepo.stubs.find;
const pen = createProduct({name: 'pen', categoryId: 1});
const pencil = createProduct({name: 'pencil', categoryId: 1});
find.resolves([pen, pencil]);
const products = await findByForeignKeys(productRepo, 'categoryId', 1);
expect(products).to.deepEqual([pen, pencil]);
sinon.assert.calledWithMatch(find, {
where: {
categoryId: 1,
},
});
});
it('does not include instances with different foreign key values', async () => {
const find = productRepo.stubs.find;
const pen = await productRepo.create({name: 'pen', categoryId: 1});
const pencil = await productRepo.create({name: 'pencil', categoryId: 2});
find.resolves([pen]);
const products = await findByForeignKeys(productRepo, 'categoryId', 1);
expect(products).to.deepEqual([pen]);
expect(products).to.not.containDeep(pencil);
sinon.assert.calledWithMatch(find, {
where: {
categoryId: 1,
},
});
});
it('includes instances when there is one value in the array of foreign key values', async () => {
const find = productRepo.stubs.find;
const pen = await productRepo.create({name: 'pen', categoryId: 1});
const pencil = await productRepo.create({name: 'pencil', categoryId: 2});
find.resolves([pencil]);
const products = await findByForeignKeys(productRepo, 'categoryId', [2]);
expect(products).to.deepEqual([pencil]);
expect(products).to.not.containDeep(pen);
sinon.assert.calledWithMatch(find, {
where: {
categoryId: 2,
},
});
});
it('returns all instances that have any of multiple foreign key values', async () => {
const pen = createProduct({name: 'pen', categoryId: 1});
const pencil = createProduct({name: 'pencil', categoryId: 2});
const paper = createProduct({name: 'paper', categoryId: 3});
const find = productRepo.stubs.find;
find.resolves([pen, paper]);
const products = await findByForeignKeys(productRepo, 'categoryId', [1, 3]);
expect(products).to.deepEqual([pen, paper]);
expect(products).to.not.containDeep(pencil);
sinon.assert.calledWithMatch(find, {
where: {
categoryId: {
inq: [1, 3],
},
},
});
});
it('does not throw an error if scope is passed in and is undefined or empty', async () => {
const find = productRepo.stubs.find;
find.resolves([]);
let products = await findByForeignKeys(
productRepo,
'categoryId',
[1],
undefined,
{},
);
expect(products).to.be.empty();
sinon.assert.calledWithMatch(find, {});
products = await findByForeignKeys(productRepo, 'categoryId', 1, {}, {});
expect(products).to.be.empty();
sinon.assert.calledWithMatch(find, {});
});
it('checks if the custom scope is handled properly', async () => {
const find = productRepo.stubs.find;
find.resolves([]);
await productRepo.create({id: 1, name: 'product', categoryId: 1});
await productRepo.create({id: 2, name: 'product', categoryId: 1});
await findByForeignKeys(productRepo, 'categoryId', 1, {
where: {id: 2},
include: [{relation: 'nested inclusion'}],
});
sinon.assert.calledWithMatch(find, {
where: {
categoryId: 1,
id: 2,
},
include: [{relation: 'nested inclusion'}],
});
});
});