Newer
Older
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
import type { RegisterSchemaPayload } from '../dto/register-schema.dto.js';
import type { TestingModule } from '@nestjs/testing';
import type {
EventAnonCredsSchemasGetAll,
EventAnonCredsSchemasGetById,
EventAnonCredsSchemasRegister,
} from '@ocm/shared';
import { Test } from '@nestjs/testing';
import { Subject, of, takeUntil } from 'rxjs';
import { NATS_CLIENT } from '../../common/constants.js';
import { SchemasController } from '../schemas.controller.js';
import { SchemasService } from '../schemas.service.js';
describe('SchemasController', () => {
const natsClientMock = {};
let controller: SchemasController;
let service: SchemasService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [SchemasController],
providers: [
{ provide: NATS_CLIENT, useValue: natsClientMock },
SchemasService,
],
}).compile();
controller = module.get<SchemasController>(SchemasController);
service = module.get<SchemasService>(SchemasService);
});
describe('getAll', () => {
it('should return a list of schemas', (done) => {
const unsubscribe$ = new Subject<void>();
const tenantId = 'exampleTenantId';
const expectedResult: EventAnonCredsSchemasGetAll['data'] = [];
jest.spyOn(service, 'getAll').mockReturnValue(of(expectedResult));
controller
.getAll({ tenantId })
.pipe(takeUntil(unsubscribe$))
.subscribe((result) => {
expect(result).toStrictEqual(expectedResult);
unsubscribe$.next();
unsubscribe$.complete();
done();
});
});
});
describe('getById', () => {
it('should return a schema by id', (done) => {
const unsubscribe$ = new Subject<void>();
const schemaId = 'exampleSchemaId';
const tenantId = 'exampleTenantId';
const expectedResult: EventAnonCredsSchemasGetById['data'] = {
attrNames: ['exampleAttributeName'],
issuerId: 'exampleIssuerDid',
name: 'exampleName',
version: '1.0.0',
};
jest.spyOn(service, 'getById').mockReturnValue(of(expectedResult));
controller
.getById({ schemaId }, { tenantId })
.pipe(takeUntil(unsubscribe$))
.subscribe((result) => {
expect(result).toStrictEqual(expectedResult);
unsubscribe$.next();
unsubscribe$.complete();
done();
});
});
it('should throw a NotFoundException if the service returned null', (done) => {
const unsubscribe$ = new Subject<void>();
const schemaId = 'exampleSchemaId';
const tenantId = 'exampleTenantId';
jest.spyOn(service, 'getById').mockReturnValue(of(null));
controller
.getById({ schemaId }, { tenantId })
.pipe(takeUntil(unsubscribe$))
.subscribe({
error: (error) => {
expect(error.status).toBe(404);
expect(error.message).toBe(`Schema with id ${schemaId} not found`);
unsubscribe$.next();
unsubscribe$.complete();
done();
},
});
});
});
describe('register', () => {
it('should register a new schema', (done) => {
const unsubscribe$ = new Subject<void>();
const tenantId = 'exampleTenantId';
const payload: RegisterSchemaPayload = {
attributeNames: ['exampleAttributeName'],
issuerDid: 'exampleIssuerDid',
name: 'exampleName',
version: '1.0.0',
};
const expectedResult: EventAnonCredsSchemasRegister['data'] = {
attrNames: payload.attributeNames,
issuerId: payload.issuerDid,
name: payload.name,
version: payload.version,
};
jest.spyOn(service, 'register').mockReturnValue(of(expectedResult));
controller
.register({ tenantId }, payload)
.pipe(takeUntil(unsubscribe$))
.subscribe((result) => {
expect(result).toStrictEqual(expectedResult);
unsubscribe$.next();
unsubscribe$.complete();
done();
});
});
});
});