-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.spec.ts
89 lines (78 loc) · 2.71 KB
/
index.spec.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
79
80
81
82
83
84
85
86
87
88
89
import { describe, expect, test } from '@jest/globals';
import bundle from '../../src';
import { isExternalReference } from '../../src/parser';
import fs from 'fs';
import path from 'path';
import type { ReferenceObject } from '../../src/spec-types';
describe('[integration testing] bundler should ', () => {
test('should return bundled doc', async () => {
const files = ['./tests/camera.yml', './tests/audio.yml'];
const response = await bundle(
files.map(file =>
fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')
),
{
base: fs.readFileSync(
path.resolve(process.cwd(), './tests/base.yml'),
'utf-8'
),
validate: false,
}
);
expect(response).toBeDefined();
});
test('should bundle references into components', async () => {
const files = ['./tests/asyncapi.yaml'];
const doc = await bundle(
files.map(file =>
fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')
),
{
referenceIntoComponents: true,
}
);
const asyncapiObject = doc.json();
const message = asyncapiObject.channels?.['user/signedup']?.subscribe?.message as ReferenceObject;
expect(message.$ref).toMatch('#/components/messages/UserSignedUp');
});
test('should not throw if value of `$ref` is not a string', async () => {
const files = ['./tests/wrong-ref-not-string.yaml'];
// If async function `bundle()` resolved Promise successfully, that means it
// did not throw exception during process of execution, which is the
// objective of testing.
expect(
await bundle(
files.map(file =>
fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')
),
{
referenceIntoComponents: true,
}
)
).resolves;
});
test('should not throw if value of `$ref` is absent', async () => {
const files = ['./tests/wrong-ref-absent.yaml'];
// If async function `bundle()` resolved Promise successfully, that means it
// did not throw exception during process of execution, which is the
// objective of testing.
expect(
await bundle(
files.map(file =>
fs.readFileSync(path.resolve(process.cwd(), file), 'utf-8')
),
{
referenceIntoComponents: true,
}
)
).resolves;
});
});
describe('[unit testing]', () => {
test('`isExternalReference()` should return `true` on external reference', () => {
expect(isExternalReference('./components/messages/UserSignedUp')).toBeTruthy();
});
test('`isExternalReference()` should return `false` on local reference', () => {
expect(isExternalReference('#/components/messages/UserSignedUp')).toBeFalsy();
});
});