|
3 | 3 | // This file is licensed under the MIT License. |
4 | 4 | // License text available at https://opensource.org/licenses/MIT |
5 | 5 |
|
6 | | -import {Entity, model, property} from '@loopback/repository'; |
| 6 | +import { |
| 7 | + Entity, |
| 8 | + EntityCrudRepository, |
| 9 | + Filter, |
| 10 | + model, |
| 11 | + property, |
| 12 | +} from '@loopback/repository'; |
7 | 13 | import {expect} from '@loopback/testlab'; |
8 | | -import {defineCrudRestController} from '../..'; |
| 14 | +import {CrudRestControllerOptions, defineCrudRestController} from '../..'; |
9 | 15 |
|
10 | 16 | describe('defineCrudRestController', () => { |
11 | 17 | it('should generate controller based on Model name', async () => { |
@@ -45,4 +51,38 @@ describe('defineCrudRestController', () => { |
45 | 51 | // It must generate read methods |
46 | 52 | expect(CrudRestController.prototype.find).not.equal(undefined); |
47 | 53 | }); |
| 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 | + }); |
48 | 88 | }); |
0 commit comments