-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
relation.repository.unit.ts
152 lines (137 loc) · 4.53 KB
/
relation.repository.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
// 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 {Getter} from '@loopback/context';
import {
createStubInstance,
expect,
sinon,
StubbedInstanceWithSinonAccessor,
} from '@loopback/testlab';
import {
AnyObject,
Count,
DataObject,
DefaultCrudRepository,
DefaultHasManyRepository,
Entity,
Filter,
HasManyRepository,
juggler,
Options,
Where,
} from '../../..';
describe('relation repository', () => {
let customerRepo: StubbedInstanceWithSinonAccessor<CustomerRepository>;
beforeEach(setupStubbedCustomerRepository);
context('HasManyRepository interface', () => {
/**
* The class below is declared as test for the HasManyEntityCrudRepository
* interface. The TS Compiler will complain if the interface changes.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class TestHasManyEntityCrudRepository<T extends Entity>
implements HasManyRepository<T> {
create(
targetModelData: DataObject<T>,
options?: AnyObject | undefined,
): Promise<T> {
/* istanbul ignore next */
throw new Error('Method not implemented.');
}
find(
filter?: Filter | undefined,
options?: AnyObject | undefined,
): Promise<T[]> {
/* istanbul ignore next */
throw new Error('Method not implemented.');
}
async delete(where?: Where, options?: Options): Promise<Count> {
/* istanbul ignore next */
throw new Error('Method not implemented.');
}
async patch(
dataObject: DataObject<T>,
where?: Where,
options?: Options,
): Promise<Count> {
/* istanbul ignore next */
throw new Error('Method not implemented.');
}
}
});
context('DefaultHasManyEntityCrudRepository', () => {
it('can create related model instance', async () => {
const constraint: Partial<Customer> = {age: 25};
const hasManyCrudInstance = givenDefaultHasManyInstance(constraint);
await hasManyCrudInstance.create({id: 1, name: 'Joe'});
sinon.assert.calledWithMatch(customerRepo.stubs.create, {
id: 1,
name: 'Joe',
age: 25,
});
});
it('can find related model instance', async () => {
const constraint: Partial<Customer> = {name: 'Jane'};
const hasManyCrudInstance = givenDefaultHasManyInstance(constraint);
await hasManyCrudInstance.find({where: {id: 3}});
sinon.assert.calledWithMatch(customerRepo.stubs.find, {
where: {id: 3, name: 'Jane'},
});
});
context('patch', () => {
it('can patch related model instance', async () => {
const constraint: Partial<Customer> = {name: 'Jane'};
const hasManyCrudInstance = givenDefaultHasManyInstance(constraint);
await hasManyCrudInstance.patch({country: 'US'}, {id: 3});
sinon.assert.calledWith(
customerRepo.stubs.updateAll,
{country: 'US', name: 'Jane'},
{id: 3, name: 'Jane'},
);
});
it('cannot override the constrain data', async () => {
const constraint: Partial<Customer> = {name: 'Jane'};
const hasManyCrudInstance = givenDefaultHasManyInstance(constraint);
await expect(
hasManyCrudInstance.patch({name: 'Joe'}),
).to.be.rejectedWith(/Property "name" cannot be changed!/);
});
});
it('can delete related model instance', async () => {
const constraint: Partial<Customer> = {name: 'Jane'};
const hasManyCrudInstance = givenDefaultHasManyInstance(constraint);
await hasManyCrudInstance.delete({id: 3});
sinon.assert.calledWith(customerRepo.stubs.deleteAll, {
id: 3,
name: 'Jane',
});
});
});
/*------------- HELPERS ---------------*/
class Customer extends Entity {
id: number;
name: string;
age: number;
country: string;
}
class CustomerRepository extends DefaultCrudRepository<
Customer,
typeof Customer.prototype.id
> {
constructor(dataSource: juggler.DataSource) {
super(Customer, dataSource);
}
}
function setupStubbedCustomerRepository() {
customerRepo = createStubInstance(CustomerRepository);
}
function givenDefaultHasManyInstance(constraint: DataObject<Customer>) {
return new DefaultHasManyRepository<
Customer,
typeof Customer.prototype.id,
CustomerRepository
>(Getter.fromValue(customerRepo), constraint);
}
});