Skip to content

Commit fcc2690

Browse files
committed
test: add unit test to verify options parameter
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
1 parent 17ceca7 commit fcc2690

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

‎packages/rest-crud/src/__tests__/unit/crud-rest-controller.unit.ts‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import {Entity, model, property} from '@loopback/repository';
6+
import {
7+
Entity,
8+
EntityCrudRepository,
9+
Filter,
10+
model,
11+
property,
12+
} from '@loopback/repository';
713
import {expect} from '@loopback/testlab';
8-
import {defineCrudRestController} from '../..';
14+
import {CrudRestControllerOptions, defineCrudRestController} from '../..';
915

1016
describe('defineCrudRestController', () => {
1117
it('should generate controller based on Model name', async () => {
@@ -45,4 +51,38 @@ describe('defineCrudRestController', () => {
4551
// It must generate read methods
4652
expect(CrudRestController.prototype.find).not.equal(undefined);
4753
});
54+
55+
it('passes options parameter to repository methods', async () => {
56+
@model()
57+
class Product extends Entity {
58+
@property({id: true})
59+
id: number;
60+
}
61+
62+
const controllerOptions = {
63+
basePath: '/products',
64+
};
65+
66+
const CrudRestController = defineCrudRestController<
67+
Product,
68+
typeof Product.prototype.id,
69+
'id'
70+
>(Product, controllerOptions);
71+
72+
let passedOptions: CrudRestControllerOptions = {basePath: '/'};
73+
const mockRepo = {
74+
find: async (
75+
filter: Filter<Product>,
76+
options: CrudRestControllerOptions,
77+
) => {
78+
passedOptions = options;
79+
return [];
80+
},
81+
} as unknown as EntityCrudRepository<Product, typeof Product.prototype.id>;
82+
83+
const controller = new CrudRestController(mockRepo);
84+
85+
await controller.find();
86+
expect(passedOptions).to.deepEqual(controllerOptions);
87+
});
4888
});

0 commit comments

Comments
 (0)