-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfreeform-properties.suite.ts
78 lines (68 loc) · 2.41 KB
/
freeform-properties.suite.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
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/repository-tests
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Entity, model, property} from '@loopback/repository';
import {EntityCrudRepository} from '@loopback/repository/src';
import {expect, skipIf, toJSON} from '@loopback/testlab';
import {Suite} from 'mocha';
import {
withCrudCtx,
deleteAllModelsInDefaultDataSource,
} from '../helpers.repository-tests';
import {
CrudFeatures,
CrudRepositoryCtor,
CrudTestContext,
DataSourceOptions,
} from '../types.repository-tests';
export function freeformPropertiesSuite(
dataSourceOptions: DataSourceOptions,
repositoryClass: CrudRepositoryCtor,
features: CrudFeatures,
) {
skipIf<[(this: Suite) => void], void>(
!features.freeFormProperties,
describe,
'free-form properties (strict: false)',
() => {
before(deleteAllModelsInDefaultDataSource);
@model({settings: {strict: false}})
class Freeform extends Entity {
@property({
type: features.idType,
id: true,
description: 'The unique identifier for a product',
})
id: number | string;
@property({type: 'string', required: true})
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[propName: string]: any;
constructor(data?: Partial<Freeform>) {
super(data);
}
}
let repo: EntityCrudRepository<Freeform, typeof Freeform.prototype.id>;
before(
withCrudCtx(async function setupRepository(ctx: CrudTestContext) {
repo = new repositoryClass(Freeform, ctx.dataSource);
await ctx.dataSource.automigrate(Freeform.name);
}),
);
it('stores extra properties on create', async () => {
// FIXME(bajtos) Fix repository types so that explicit cast can be removed
// ATM, compiler complains that {name: 'Pencil', color: 'red'} cannot be
// assigned to DeepPartial<Product>
const created = await repo.create(<Partial<Freeform>>{
name: 'Pencil',
color: 'red',
});
expect(created.toObject()).to.have.properties('id', 'name');
expect(created.id).to.be.ok();
const found = await repo.findById(created.id);
expect(toJSON(created)).to.deepEqual(toJSON(found));
});
},
);
}