@@ -6,28 +6,32 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
6
6
7
7
import process from 'node:process' ;
8
8
import { AxiosError } from 'axios' ;
9
- import { MCDocument } from './types.js' ;
9
+ import type { MCDocument } from './types.js' ;
10
10
11
11
let testProjectId = '316557b3-cb6f-47ed-acf7-fcfb7ce188d5' ;
12
12
let baseURL = new URL ( 'https://test.mermaidchart.com' ) ;
13
13
14
14
let client : MermaidChart ;
15
15
16
- beforeAll ( async ( ) => {
16
+ beforeAll ( async ( ) => {
17
17
if ( process . env . TEST_MERMAIDCHART_BASE_URL ) {
18
18
try {
19
19
baseURL = new URL ( process . env . TEST_MERMAIDCHART_BASE_URL ) ;
20
20
} catch ( err ) {
21
- throw new Error ( "Invalid URL in environment variable TEST_MERMAIDCHART_BASE_URL" , { cause : err } ) ;
21
+ throw new Error ( 'Invalid URL in environment variable TEST_MERMAIDCHART_BASE_URL' , {
22
+ cause : err ,
23
+ } ) ;
22
24
}
23
25
} else {
24
- process . emitWarning ( `Missing environment variable TEST_MERMAIDCHART_BASE_URL. Defaulting to ${ baseURL . href } .` ) ;
26
+ process . emitWarning (
27
+ `Missing environment variable TEST_MERMAIDCHART_BASE_URL. Defaulting to ${ baseURL . href } .` ,
28
+ ) ;
25
29
}
26
30
27
31
if ( ! process . env . TEST_MERMAIDCHART_API_TOKEN ) {
28
32
throw new Error (
29
- " Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. "
30
- + `Please go to ${ new URL ( '/app/user/settings' , baseURL ) } and create one.`
33
+ ' Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. ' +
34
+ `Please go to ${ new URL ( '/app/user/settings' , baseURL ) } and create one.` ,
31
35
) ;
32
36
}
33
37
@@ -44,25 +48,30 @@ beforeAll(async() => {
44
48
// confirm that testProjectId is valid
45
49
if ( process . env . TEST_MERMAIDCHART_PROJECT_ID ) {
46
50
testProjectId = process . env . TEST_MERMAIDCHART_PROJECT_ID ;
47
- if ( ! projects . find ( ( project ) => project . id === testProjectId ) ) {
51
+ if ( ! projects . some ( ( project ) => project . id === testProjectId ) ) {
48
52
throw new Error (
49
- `Invalid environment variable TEST_MERMAIDCHART_PROJECT_ID. `
50
- + `Please go to ${ new URL ( '/app/projects' , baseURL ) } and create one that you have access to.`
53
+ `Invalid environment variable TEST_MERMAIDCHART_PROJECT_ID. ` +
54
+ `Please go to ${ new URL (
55
+ '/app/projects' ,
56
+ baseURL ,
57
+ ) } and create one that you have access to.`,
51
58
) ;
52
59
}
53
60
} else {
54
- if ( ! projects . find ( ( project ) => project . id === testProjectId ) ) {
61
+ if ( ! projects . some ( ( project ) => project . id === testProjectId ) ) {
55
62
throw new Error (
56
- `Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. `
57
- + `Please go to ${ new URL ( '/app/projects' , baseURL ) } and create one.`
63
+ `Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. ` +
64
+ `Please go to ${ new URL ( '/app/projects' , baseURL ) } and create one.` ,
58
65
) ;
59
66
}
60
- process . emitWarning ( `Missing optional environment variable TEST_MERMAIDCHART_PROJECT_ID. Defaulting to ${ testProjectId } ` ) ;
67
+ process . emitWarning (
68
+ `Missing optional environment variable TEST_MERMAIDCHART_PROJECT_ID. Defaulting to ${ testProjectId } ` ,
69
+ ) ;
61
70
}
62
71
} ) ;
63
72
64
73
describe ( 'getUser' , ( ) => {
65
- it ( " should get user" , async ( ) => {
74
+ it ( ' should get user' , async ( ) => {
66
75
const user = await client . getUser ( ) ;
67
76
68
77
expect ( user ) . toHaveProperty ( 'emailAddress' ) ;
@@ -78,17 +87,19 @@ const documentMatcher = expect.objectContaining({
78
87
/**
79
88
* Cleanup created documents at the end of this test.
80
89
*/
81
- const documentsToDelete = new Set < MCDocument [ "documentID" ] > ( ) ;
82
- afterAll ( async ( ) => {
83
- await Promise . all ( Array . from ( documentsToDelete ) . map ( async ( document ) => {
84
- if ( documentsToDelete . delete ( document ) ) {
85
- await client . deleteDocument ( document ) ;
86
- }
87
- } ) ) ;
90
+ const documentsToDelete = new Set < MCDocument [ 'documentID' ] > ( ) ;
91
+ afterAll ( async ( ) => {
92
+ await Promise . all (
93
+ [ ...documentsToDelete ] . map ( async ( document ) => {
94
+ if ( documentsToDelete . delete ( document ) ) {
95
+ await client . deleteDocument ( document ) ;
96
+ }
97
+ } ) ,
98
+ ) ;
88
99
} ) ;
89
100
90
101
describe ( 'createDocument' , ( ) => {
91
- it ( 'should create document in project' , async ( ) => {
102
+ it ( 'should create document in project' , async ( ) => {
92
103
const existingDocuments = await client . getDocuments ( testProjectId ) ;
93
104
94
105
const newDocument = await client . createDocument ( testProjectId ) ;
@@ -105,7 +116,7 @@ describe('createDocument', () => {
105
116
} ) ;
106
117
107
118
describe ( 'setDocument' , ( ) => {
108
- it ( 'should set document' , async ( ) => {
119
+ it ( 'should set document' , async ( ) => {
109
120
const newDocument = await client . createDocument ( testProjectId ) ;
110
121
documentsToDelete . add ( newDocument . documentID ) ;
111
122
@@ -115,33 +126,35 @@ describe('setDocument', () => {
115
126
await client . setDocument ( {
116
127
documentID : newDocument . documentID ,
117
128
projectID : newDocument . projectID ,
118
- title : " @mermaidchart/sdk E2E test diagram" ,
129
+ title : ' @mermaidchart/sdk E2E test diagram' ,
119
130
code,
120
131
} ) ;
121
132
122
133
const updatedDoc = await client . getDocument ( {
123
134
documentID : newDocument . documentID ,
124
135
} ) ;
125
136
expect ( updatedDoc ) . toMatchObject ( {
126
- title : " @mermaidchart/sdk E2E test diagram" ,
137
+ title : ' @mermaidchart/sdk E2E test diagram' ,
127
138
code,
128
139
} ) ;
129
140
} ) ;
130
141
131
- it ( 'should throw an error on invalid data' , async ( ) => {
142
+ it ( 'should throw an error on invalid data' , async ( ) => {
132
143
const newDocument = await client . createDocument ( testProjectId ) ;
133
144
documentsToDelete . add ( newDocument . documentID ) ;
134
145
135
- await expect ( client . setDocument ( {
136
- documentID : newDocument . documentID ,
137
- // @ts -expect-error not setting diagram `projectID` should throw an error
138
- projectID : null ,
139
- } ) ) . rejects . toThrowError ( "400" ) ; // should throw HTTP 400 error
146
+ await expect (
147
+ client . setDocument ( {
148
+ documentID : newDocument . documentID ,
149
+ // @ts -expect-error not setting diagram `projectID` should throw an error
150
+ projectID : null ,
151
+ } ) ,
152
+ ) . rejects . toThrowError ( '400' ) ; // should throw HTTP 400 error
140
153
} ) ;
141
154
} ) ;
142
155
143
156
describe ( 'deleteDocument' , ( ) => {
144
- it ( 'should delete document' , async ( ) => {
157
+ it ( 'should delete document' , async ( ) => {
145
158
const newDocument = await client . createDocument ( testProjectId ) ;
146
159
147
160
expect ( await client . getDocuments ( testProjectId ) ) . toContainEqual ( newDocument ) ;
@@ -154,8 +167,8 @@ describe('deleteDocument', () => {
154
167
} ) ;
155
168
} ) ;
156
169
157
- describe ( " getDocument" , ( ) => {
158
- it ( " should get diagram" , async ( ) => {
170
+ describe ( ' getDocument' , ( ) => {
171
+ it ( ' should get diagram' , async ( ) => {
159
172
const newDocument = await client . createDocument ( testProjectId ) ;
160
173
161
174
documentsToDelete . add ( newDocument . documentID ) ;
@@ -176,7 +189,7 @@ describe("getDocument", () => {
176
189
expect ( earliestDocument ) . toStrictEqual ( documentMatcher ) ;
177
190
} ) ;
178
191
179
- it ( " should throw 404 on unknown document" , async ( ) => {
192
+ it ( ' should throw 404 on unknown document' , async ( ) => {
180
193
let error : AxiosError | undefined = undefined ;
181
194
try {
182
195
await client . getDocument ( {
@@ -190,4 +203,3 @@ describe("getDocument", () => {
190
203
expect ( error ?. response ?. status ) . toBe ( 404 ) ;
191
204
} ) ;
192
205
} ) ;
193
-
0 commit comments