@@ -3,15 +3,23 @@ import { getRepositoryToken } from '@nestjs/typeorm';
3
3
import { Repository } from 'typeorm' ;
4
4
import {
5
5
mockAgreements ,
6
- mockAgreementsEntity , mockFeaturesConfig , mockFeaturesConfigEntity , mockFeaturesConfigId , mockFeaturesConfigJson ,
6
+ mockAgreementsEntity ,
7
+ mockFeaturesConfig ,
8
+ mockFeaturesConfigData ,
9
+ mockFeaturesConfigEntity ,
10
+ mockFeaturesConfigId ,
11
+ mockFeaturesConfigJson ,
7
12
mockRepository ,
8
- MockType , mockUserId
13
+ MockType ,
14
+ mockUserId
9
15
} from 'src/__mocks__' ;
10
16
import { AgreementsEntity } from 'src/modules/settings/entities/agreements.entity' ;
11
17
import { LocalFeaturesConfigRepository } from 'src/modules/feature/repositories/local.features-config.repository' ;
12
18
import { FeaturesConfigEntity } from 'src/modules/feature/entities/features-config.entity' ;
13
19
import { classToPlain , plainToClass } from 'class-transformer' ;
14
20
import { FeaturesConfigData } from 'src/modules/feature/model/features-config' ;
21
+ import * as defaultConfig from '../../../../config/features-config.json' ;
22
+
15
23
describe ( 'LocalFeaturesConfigRepository' , ( ) => {
16
24
let service : LocalFeaturesConfigRepository ;
17
25
let repository : MockType < Repository < FeaturesConfigEntity > > ;
@@ -33,7 +41,7 @@ describe('LocalFeaturesConfigRepository', () => {
33
41
service = await module . get ( LocalFeaturesConfigRepository ) ;
34
42
35
43
repository . findOneBy . mockResolvedValue ( mockFeaturesConfigEntity ) ;
36
- repository . update . mockResolvedValue ( true ) ; // no meter of response
44
+ repository . update . mockResolvedValue ( mockFeaturesConfigEntity ) ;
37
45
repository . save . mockResolvedValue ( mockFeaturesConfigEntity ) ;
38
46
} ) ;
39
47
@@ -73,34 +81,34 @@ describe('LocalFeaturesConfigRepository', () => {
73
81
} ) ;
74
82
75
83
describe ( 'getOrCreate' , ( ) => {
76
- it ( 'ttt' , async ( ) => {
84
+ it ( 'should return existing config' , async ( ) => {
85
+ const result = await service . getOrCreate ( ) ;
86
+
87
+ expect ( result ) . toEqual ( mockFeaturesConfig ) ;
77
88
} ) ;
78
- // it('should return existing config', async () => {
79
- // const result = await service.getOrCreate();
80
- //
81
- // expect(result).toEqual(mockFeaturesConfig);
82
- // });
83
- // it('should create new config', async () => {
84
- // repository.findOneBy.mockResolvedValueOnce(null);
85
- //
86
- // const result = await service.getOrCreate();
87
- //
88
- // expect(result).toEqual({
89
- // ...mockAgreements,
90
- // version: undefined,
91
- // data: undefined,
92
- // });
93
- // });
94
- } ) ;
89
+ it ( 'should update existing config with newest default' , async ( ) => {
90
+ repository . findOneBy . mockResolvedValueOnce ( plainToClass ( FeaturesConfigEntity , {
91
+ ...mockFeaturesConfig ,
92
+ data : {
93
+ ...mockFeaturesConfigData ,
94
+ version : defaultConfig . version - 0.1 ,
95
+ } ,
96
+ } ) ) ;
97
+
98
+ const result = await service . getOrCreate ( ) ;
95
99
96
- // describe('update', () => {
97
- // it('should update agreements', async () => {
98
- // const result = await service.update(mockUserId, mockAgreements);
99
- //
100
- // expect(result).toEqual(mockAgreements);
101
- // expect(repository.update).toHaveBeenCalledWith({}, {
102
- // ...mockAgreementsEntity,
103
- // });
104
- // });
105
- // });
100
+ expect ( result ) . toEqual ( mockFeaturesConfig ) ;
101
+ expect ( repository . update ) . toHaveBeenCalledWith (
102
+ { id : service [ 'id' ] } ,
103
+ plainToClass ( FeaturesConfigEntity , { data : defaultConfig } ) ,
104
+ ) ;
105
+ } ) ;
106
+ it ( 'should create new config' , async ( ) => {
107
+ repository . findOneBy . mockResolvedValueOnce ( null ) ;
108
+
109
+ const result = await service . getOrCreate ( ) ;
110
+
111
+ expect ( result ) . toEqual ( mockFeaturesConfig ) ;
112
+ } ) ;
113
+ } ) ;
106
114
} ) ;
0 commit comments