Skip to content

Commit 4f20f15

Browse files
committed
refactored tests
1 parent 9d0ff46 commit 4f20f15

6 files changed

Lines changed: 382 additions & 678 deletions

File tree

packages/b2c-cli/test/commands/ods/delete.test.ts

Lines changed: 65 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
import {expect} from 'chai';
88
/* eslint-disable @typescript-eslint/no-explicit-any */
99
import OdsDelete from '../../../src/commands/ods/delete.js';
10+
import {
11+
makeCommandThrowOnError,
12+
stubCommandConfigAndLogger,
13+
stubJsonEnabled,
14+
stubOdsClient,
15+
} from '../../helpers/ods.js';
1016

1117
/**
1218
* Unit tests for ODS delete command CLI logic.
@@ -52,14 +58,9 @@ describe('ods delete', () => {
5258
configurable: true,
5359
});
5460

55-
// Mock logger
56-
Object.defineProperty(command, 'logger', {
57-
value: {info() {}, debug() {}, warn() {}, error() {}},
58-
configurable: true,
59-
});
60-
6161
(command as any).flags = {force: true};
62-
command.jsonEnabled = () => true;
62+
stubCommandConfigAndLogger(command);
63+
stubJsonEnabled(command, true);
6364

6465
const logs: string[] = [];
6566
command.log = (msg?: string) => {
@@ -73,18 +74,15 @@ describe('ods delete', () => {
7374
operationState: 'running' as const,
7475
};
7576

76-
Object.defineProperty(command, 'odsClient', {
77-
value: {
78-
GET: async () => ({
79-
data: {data: {id: 'sandbox-123', realm: 'zzzv', instance: 'zzzv-001'}},
80-
response: new Response(),
81-
}),
82-
DELETE: async () => ({
83-
data: {data: mockOperation},
84-
response: new Response(null, {status: 202}),
85-
}),
86-
},
87-
configurable: true,
77+
stubOdsClient(command, {
78+
GET: async () => ({
79+
data: {data: {id: 'sandbox-123', realm: 'zzzv', instance: 'zzzv-001'}},
80+
response: new Response(),
81+
}),
82+
DELETE: async () => ({
83+
data: {data: mockOperation},
84+
response: new Response(null, {status: 202}),
85+
}),
8886
});
8987

9088
await command.run();
@@ -102,31 +100,24 @@ describe('ods delete', () => {
102100
configurable: true,
103101
});
104102

105-
Object.defineProperty(command, 'logger', {
106-
value: {info() {}, debug() {}, warn() {}, error() {}},
107-
configurable: true,
108-
});
109-
110103
(command as any).flags = {force: true};
111-
command.jsonEnabled = () => false;
104+
stubCommandConfigAndLogger(command);
105+
stubJsonEnabled(command, false);
112106

113107
const logs: string[] = [];
114108
command.log = (msg?: string) => {
115109
if (msg !== undefined) logs.push(msg);
116110
};
117111

118-
Object.defineProperty(command, 'odsClient', {
119-
value: {
120-
GET: async () => ({
121-
data: {data: {id: 'sandbox-123', realm: 'zzzv'}},
122-
response: new Response(),
123-
}),
124-
DELETE: async () => ({
125-
data: {data: {}},
126-
response: new Response(null, {status: 202}),
127-
}),
128-
},
129-
configurable: true,
112+
stubOdsClient(command, {
113+
GET: async () => ({
114+
data: {data: {id: 'sandbox-123', realm: 'zzzv'}},
115+
response: new Response(),
116+
}),
117+
DELETE: async () => ({
118+
data: {data: {}},
119+
response: new Response(null, {status: 202}),
120+
}),
130121
});
131122

132123
await command.run();
@@ -143,25 +134,14 @@ describe('ods delete', () => {
143134
configurable: true,
144135
});
145136

146-
Object.defineProperty(command, 'logger', {
147-
value: {info() {}, debug() {}, warn() {}, error() {}},
148-
configurable: true,
149-
});
150-
151137
(command as any).flags = {force: true};
152-
153-
command.error = (msg: string) => {
154-
throw new Error(msg);
155-
};
156-
157-
Object.defineProperty(command, 'odsClient', {
158-
value: {
159-
GET: async () => ({
160-
data: {data: undefined},
161-
response: new Response(null, {status: 404}),
162-
}),
163-
},
164-
configurable: true,
138+
stubCommandConfigAndLogger(command);
139+
makeCommandThrowOnError(command);
140+
stubOdsClient(command, {
141+
GET: async () => ({
142+
data: {data: undefined},
143+
response: new Response(null, {status: 404}),
144+
}),
165145
});
166146

167147
try {
@@ -180,31 +160,20 @@ describe('ods delete', () => {
180160
configurable: true,
181161
});
182162

183-
Object.defineProperty(command, 'logger', {
184-
value: {info() {}, debug() {}, warn() {}, error() {}},
185-
configurable: true,
186-
});
187-
188163
(command as any).flags = {force: true};
189-
164+
stubCommandConfigAndLogger(command);
190165
command.log = () => {};
191-
command.error = (msg: string) => {
192-
throw new Error(msg);
193-
};
194-
195-
Object.defineProperty(command, 'odsClient', {
196-
value: {
197-
GET: async () => ({
198-
data: {data: {id: 'sandbox-123', realm: 'zzzv'}},
199-
response: new Response(),
200-
}),
201-
DELETE: async () => ({
202-
data: undefined,
203-
error: {error: {message: 'Operation failed'}},
204-
response: new Response(null, {status: 500}),
205-
}),
206-
},
207-
configurable: true,
166+
makeCommandThrowOnError(command);
167+
stubOdsClient(command, {
168+
GET: async () => ({
169+
data: {data: {id: 'sandbox-123', realm: 'zzzv'}},
170+
response: new Response(),
171+
}),
172+
DELETE: async () => ({
173+
data: undefined,
174+
error: {error: {message: 'Operation failed'}},
175+
response: new Response(null, {status: 500}),
176+
}),
208177
});
209178

210179
try {
@@ -224,25 +193,14 @@ describe('ods delete', () => {
224193
configurable: true,
225194
});
226195

227-
Object.defineProperty(command, 'logger', {
228-
value: {info() {}, debug() {}, warn() {}, error() {}},
229-
configurable: true,
230-
});
231-
232196
(command as any).flags = {force: true};
233-
234-
command.error = (msg: string) => {
235-
throw new Error(msg);
236-
};
237-
238-
Object.defineProperty(command, 'odsClient', {
239-
value: {
240-
GET: async () => ({
241-
data: null as any,
242-
response: new Response(null, {status: 500}),
243-
}),
244-
},
245-
configurable: true,
197+
stubCommandConfigAndLogger(command);
198+
makeCommandThrowOnError(command);
199+
stubOdsClient(command, {
200+
GET: async () => ({
201+
data: null as any,
202+
response: new Response(null, {status: 500}),
203+
}),
246204
});
247205

248206
try {
@@ -261,30 +219,19 @@ describe('ods delete', () => {
261219
configurable: true,
262220
});
263221

264-
Object.defineProperty(command, 'logger', {
265-
value: {info() {}, debug() {}, warn() {}, error() {}},
266-
configurable: true,
267-
});
268-
269222
(command as any).flags = {force: true};
270-
223+
stubCommandConfigAndLogger(command);
271224
command.log = () => {};
272-
command.error = (msg: string) => {
273-
throw new Error(msg);
274-
};
275-
276-
Object.defineProperty(command, 'odsClient', {
277-
value: {
278-
GET: async () => ({
279-
data: {data: {id: 'sandbox-123', realm: 'zzzv'}},
280-
response: new Response(),
281-
}),
282-
DELETE: async () => ({
283-
data: {data: {}},
284-
response: new Response(null, {status: 400, statusText: 'Bad Request'}),
285-
}),
286-
},
287-
configurable: true,
225+
makeCommandThrowOnError(command);
226+
stubOdsClient(command, {
227+
GET: async () => ({
228+
data: {data: {id: 'sandbox-123', realm: 'zzzv'}},
229+
response: new Response(),
230+
}),
231+
DELETE: async () => ({
232+
data: {data: {}},
233+
response: new Response(null, {status: 400, statusText: 'Bad Request'}),
234+
}),
288235
});
289236

290237
try {

0 commit comments

Comments
 (0)