-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathindex.test.ts
More file actions
179 lines (166 loc) · 5.53 KB
/
index.test.ts
File metadata and controls
179 lines (166 loc) · 5.53 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import clientGen from "./";
import fs from "fs";
import fsx, { emptyDir } from "fs-extra";
import examples from "@open-rpc/examples";
import { promisify } from "util";
import { forEach } from "lodash";
import {
parseOpenRPCDocument,
OpenRPCDocumentDereferencingError,
} from "@open-rpc/schema-utils-js";
import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema";
const stat = promisify(fs.stat);
const rmdir = promisify(fs.rmdir);
console.error = () => "noop";
describe(`Examples to generate Js clients`, () => {
const testDir = `${process.cwd()}/test`;
beforeAll(async () => {
await emptyDir(testDir);
});
afterAll(async () => {
await fsx.emptyDir(testDir);
return await rmdir(testDir);
});
it("fails when the open rpc document is invalid", () => {
const testDocument = {
openrpcDocument: {
openrpc: "1.2.1",
info: {
version: "1",
title: "test",
},
methods: [
{
name: "foo",
params: [{ $ref: "#/components/contentDescriptors/LeFoo" }],
result: {
name: "bar",
schema: { $ref: "#/components/contentDescriptors/LeFoo" },
},
},
],
components: {
schemas: {
LeBar: { title: "LeBar", type: "string" },
},
contentDescriptors: {
LeFoo: {
name: "LeFoo",
required: true,
schema: { $ref: "#/components/schemas/LeBar" },
},
},
},
} as OpenRPC,
outDir: testDir,
components: [],
};
const genProm = clientGen(testDocument);
return expect(genProm).rejects.toBeInstanceOf(
OpenRPCDocumentDereferencingError
);
});
forEach(examples, (example: OpenRPC, exampleName: string) => {
it(`rejects configurations without outDir or outPath`, async () => {
const promGen = clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
components: [
{ type: "client", language: "typescript", name: "testclient-ts" },
],
});
expect(promGen).rejects.toBeInstanceOf(Error);
});
it(`creates a new client for example: ${exampleName} and regenerates after`, async () => {
const exampleOutDir = `${testDir}/${exampleName}`;
expect.assertions(5);
await clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
outDir: exampleOutDir,
components: [
{ type: "client", language: "rust", name: "testclient-rs" },
{ type: "client", language: "typescript", name: "testclient-ts" },
{ type: "server", language: "typescript", name: "testserver-ts" },
{ type: "docs", language: "gatsby", name: "testserver-gatsby" },
{
type: "custom",
language: "typescript",
name: "custom-stuff",
customComponent: "./src/custom-test-component.js",
customType: "client",
},
{
type: "custom",
language: "typescript",
name: "custom-stuff2",
customComponent: "./src/custom-test-component.js",
customType: "client",
openRPCPath: null,
},
{
type: "custom",
language: "typescript",
name: "custom-stuff3",
customComponent: "./src/custom-test-component.js",
customType: "client",
openRPCPath: "tmpz",
},
{
type: "custom",
language: "typescript",
name: "custom-stuff4",
customComponent: "./src/custom-test-component.js",
customType: "client",
openRPCPath: "tmpy",
outPath: `${exampleOutDir}/special`,
},
],
});
await expect(stat(exampleOutDir)).resolves.toBeTruthy();
await expect(stat(`${exampleOutDir}/special`)).resolves.toBeTruthy();
await clientGen({
openrpcDocument: await parseOpenRPCDocument(example),
outDir: exampleOutDir,
components: [
{ type: "client", language: "rust", name: "testclient-rs" },
{ type: "client", language: "typescript", name: "testclient-ts" },
{ type: "server", language: "typescript", name: "testserver-ts" },
{ type: "docs", language: "gatsby", name: "testserver-gatsby" },
{
type: "custom",
language: "typescript",
name: "custom-stuff",
customComponent: "./src/custom-test-component.js",
customType: "client",
},
{
type: "custom",
language: "typescript",
name: "custom-stuff2",
customComponent: "./src/custom-test-component.js",
customType: "client",
openRPCPath: null,
},
{
type: "custom",
language: "typescript",
name: "custom-stuff3",
customComponent: "./src/custom-test-component.js",
customType: "client",
openRPCPath: "tmpz",
},
{
type: "custom",
language: "typescript",
name: "custom-stuff4",
customComponent: "./src/custom-test-component.js",
customType: "client",
openRPCPath: "tmpy",
outPath: `${exampleOutDir}/special`,
},
],
});
await expect(stat(`${exampleOutDir}/special`)).resolves.toBeTruthy();
await expect(stat(exampleOutDir)).resolves.toBeTruthy();
}, 100000);
});
});