@sapui5/sap.fe.test 1.104.1 → 1.105.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,344 +1,481 @@
1
- /**
2
- * Mock class for a V4 Context
3
- */
4
- import type Event from "sap/ui/base/Event";
5
- import type Control from "sap/ui/core/Control";
6
- import type UI5Element from "sap/ui/core/Element";
7
- import type Controller from "sap/ui/core/mvc/Controller";
8
- import type View from "sap/ui/core/mvc/View";
9
- import type CompositeBinding from "sap/ui/model/CompositeBinding";
10
- import type JSONModel from "sap/ui/model/json/JSONModel";
11
- import type Context from "sap/ui/model/odata/v4/Context";
12
- import type ODataContextBinding from "sap/ui/model/odata/v4/ODataContextBinding";
13
- import type ODataListBinding from "sap/ui/model/odata/v4/ODataListBinding";
1
+ import type { WithMock } from "@sap-ux/jest-mock-ui5/dist/generic";
2
+ import { mock } from "@sap-ux/jest-mock-ui5/dist/generic";
3
+ import EditFlow from "sap/fe/core/controllerextensions/EditFlow";
4
+ import InternalEditFlow from "sap/fe/core/controllerextensions/InternalEditFlow";
5
+ import InternalRouting from "sap/fe/core/controllerextensions/InternalRouting";
6
+ import SideEffects from "sap/fe/core/controllerextensions/SideEffects";
7
+ import Event from "sap/ui/base/Event";
8
+ import Controller from "sap/ui/core/mvc/Controller";
9
+ import View from "sap/ui/core/mvc/View";
10
+ import CompositeBinding from "sap/ui/model/CompositeBinding";
11
+ import Context from "sap/ui/model/odata/v4/Context";
12
+ import ODataContextBinding from "sap/ui/model/odata/v4/ODataContextBinding";
13
+ import ODataListBinding from "sap/ui/model/odata/v4/ODataListBinding";
14
14
  import ODataMetaModel from "sap/ui/model/odata/v4/ODataMetaModel";
15
- import type ODataModel from "sap/ui/model/odata/v4/ODataModel";
16
- import type ODataPropertyBinding from "sap/ui/model/odata/v4/ODataPropertyBinding";
17
-
18
- export class MockContext implements Partial<Context> {
19
- private isKeptAlive = false;
20
- public constructor(private oValues?: any, private oBinding?: any) {}
15
+ import ODataModel from "sap/ui/model/odata/v4/ODataModel";
16
+ import ODataPropertyBinding from "sap/ui/model/odata/v4/ODataPropertyBinding";
21
17
 
22
- public isA(sClassName: string): boolean {
18
+ /**
19
+ * Utility type to mock a sap.ui.model.odata.v4.Context
20
+ */
21
+ export type MockContext = WithMock<Context> & {
22
+ _isKeptAlive: boolean;
23
+ _contextData: any;
24
+ _oBinding: any;
25
+ _isInactive: boolean;
26
+ };
27
+ /**
28
+ * Factory function to create a new MockContext.
29
+ *
30
+ * @param oContextData A map of the different properties of the context. The value for the key '$path' will be returned by the 'getPath' method
31
+ * @param oBinding The binding of the context
32
+ * @param isInactive Is the context iniactive or not
33
+ * @returns A new MockContext
34
+ */
35
+ export function createMockContext(oContextData?: any, oBinding?: any, isInactive?: boolean): MockContext {
36
+ // Ugly workaround to get a proper mock pbject, as Context isn't properly exported from UI5
37
+ const mocked = mock(Object.getPrototypeOf((Context as any).createNewContext(null, null, "/e"))) as MockContext;
38
+ mocked._isKeptAlive = false;
39
+ mocked._contextData = oContextData || {};
40
+ mocked._oBinding = oBinding;
41
+ mocked._isInactive = !!isInactive;
42
+
43
+ // Default behavior
44
+ mocked.mock.isA.mockImplementation((sClassName: string) => {
23
45
  return sClassName === "sap.ui.model.odata.v4.Context";
24
- }
25
-
26
- public getProperty = jest.fn((key: string) => {
27
- return this.oValues[key];
28
46
  });
29
- public requestProperty = jest.fn((key: string) => {
30
- return Promise.resolve(this.oValues[key]);
47
+ mocked.mock.getProperty.mockImplementation((key: string) => {
48
+ return mocked._contextData[key];
49
+ });
50
+ mocked.mock.requestProperty.mockImplementation((key: string) => {
51
+ return Promise.resolve(mocked._contextData[key]);
31
52
  });
32
- public setProperty = jest.fn((key: string, value: any) => {
33
- this.oValues[key] = value;
34
- return this.oValues[key];
53
+ mocked.mock.requestObject.mockImplementation((key: string) => {
54
+ return Promise.resolve(mocked._contextData[key]);
55
+ });
56
+ mocked.mock.setProperty.mockImplementation((key: string, value: any) => {
57
+ mocked._contextData[key] = value;
58
+ return mocked._contextData[key];
35
59
  });
36
60
 
37
- public getObject = jest.fn((path: string) => {
38
- let result = path ? this.oValues[path] : this.oValues;
61
+ mocked.mock.getObject.mockImplementation((path: string) => {
62
+ let result = path ? mocked._contextData[path] : mocked._contextData;
39
63
 
40
64
  if (!result && path && path.indexOf("/") > -1) {
41
65
  const parts = path.split("/");
42
66
  result = parts.reduce((sum, part: any) => {
43
67
  sum = part ? sum[part] : sum;
44
68
  return sum;
45
- }, this.oValues);
69
+ }, mocked._contextData);
46
70
  }
47
71
 
48
72
  return result;
49
73
  });
50
- public getPath = jest.fn(() => {
51
- return this.oValues["$path"];
52
- });
53
74
 
54
- public getBinding = jest.fn(() => {
55
- return this.oBinding;
56
- });
57
- public getModel = jest.fn(() => {
58
- return this.oBinding?.getModel() as ODataModel;
75
+ mocked.mock.getPath.mockImplementation(() => mocked._contextData["$path"]);
76
+ mocked.mock.getBinding.mockImplementation(() => mocked._oBinding);
77
+ mocked.mock.getModel.mockImplementation(() => mocked._oBinding?.getModel());
78
+ mocked.mock.setKeepAlive.mockImplementation((bool: boolean, _fnOnBeforeDestroy?: any, _bRequestMessages?: boolean) => {
79
+ mocked._isKeptAlive = bool;
59
80
  });
60
- public isTransient = jest.fn();
61
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
62
- public setKeepAlive = jest.fn((bool: boolean, _fnOnBeforeDestroy?: any, _bRequestMessages?: boolean) => {
63
- this.isKeptAlive = bool;
64
- });
65
- public isKeepAlive = jest.fn(() => this.isKeptAlive);
66
- public requestSideEffects = jest.fn();
67
- public requestObject = jest.fn();
68
- }
81
+ mocked.mock.isKeepAlive.mockImplementation(() => mocked._isKeptAlive);
82
+ mocked.mock.isInactive.mockImplementation(() => mocked._isInactive);
69
83
 
70
- export class MockUI5Element implements Partial<UI5Element> {
71
- [key: string]: any;
72
- public bindingContext = new MockContext({});
73
- public getBinding = jest.fn();
74
- public getBindingContext = jest.fn().mockReturnValue(this.bindingContext);
75
- public getBindingInfo = jest.fn();
76
- public data = jest.fn();
77
- public getModel = jest.fn();
78
- public getParent = jest.fn();
79
- }
80
-
81
- export class MockControl extends MockUI5Element implements Partial<Control> {
82
- public getBindingContext: jest.Mock<any, any> = jest.fn();
83
- public getModel: jest.Mock<any, any> = jest.fn();
84
+ return mocked;
84
85
  }
86
+ /**
87
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockContext instead.
88
+ */
89
+ export const MockContext: new (oValues?: any, oBinding?: any, isInactive?: boolean) => MockContext = createMockContext as any;
85
90
 
86
- export class MockEvent implements Partial<Event> {
87
- public constructor(public params: { [key: string]: any } = {}, public source: MockUI5Element = new MockUI5Element()) {}
91
+ /**
92
+ * Utility type to mock a sap.ui.base.Event
93
+ */
94
+ export type MockEvent = WithMock<Event> & {
95
+ _params: { [key: string]: any };
96
+ };
97
+ /**
98
+ * Factory function to create a new MockEvent.
99
+ *
100
+ * @param params The parameters of the event
101
+ * @returns A new MockEvent
102
+ */
103
+ export function createMockEvent(params?: { [key: string]: any }): MockEvent {
104
+ const mocked = mock(Event) as MockEvent;
105
+ mocked._params = params || {};
88
106
 
89
- public getParameter = jest.fn((name) => this.params[name]);
107
+ // Default behavior
108
+ mocked.mock.getParameter.mockImplementation((name) => mocked._params[name]);
90
109
 
91
- public getSource = jest.fn(() => this.source as any);
110
+ return mocked;
92
111
  }
93
-
94
112
  /**
95
- * Mock class for OData V4 ListBinding
113
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockEvent instead.
96
114
  */
97
- export class MockListBinding implements Partial<ODataListBinding> {
98
- private aMockContexts: MockContext[];
115
+ export const MockEvent: new (params?: { [key: string]: any }) => MockEvent = createMockEvent as any;
99
116
 
100
- public constructor(aContexts?: any[], private mockModel?: MockModel) {
101
- aContexts = aContexts || [];
117
+ /**
118
+ * Utility type to mock a sap.ui.model.odata.v4.ODataListBinding
119
+ */
120
+ export type MockListBinding = WithMock<ODataListBinding> & {
121
+ _aMockContexts: MockContext[];
122
+ _mockModel?: MockModel;
123
+
124
+ /**
125
+ * Utility method to set the model of the ListBinding
126
+ */
127
+ setModel: (model: MockModel) => void;
128
+ };
129
+ /**
130
+ * Factory function to create a new MockListBinding.
131
+ *
132
+ * @param aContextData An array of objects holding the different properties of the contexts referenced by the ListBinding
133
+ * @param oMockModel The model of the ListBinding
134
+ * @returns A new MockListBinding
135
+ */
136
+ export function createMockListBinding(aContextData?: any[], oMockModel?: MockModel): MockListBinding {
137
+ const mocked = mock(ODataListBinding) as MockListBinding;
138
+ aContextData = aContextData || [];
139
+ mocked._aMockContexts = aContextData.map((contextData) => {
140
+ return createMockContext(contextData, mocked);
141
+ });
142
+ mocked._mockModel = oMockModel;
102
143
 
103
- this.aMockContexts = aContexts.map((context) => {
104
- return new MockContext(context, this);
105
- });
106
- }
144
+ // Utility API
145
+ mocked.setModel = (model: MockModel) => {
146
+ mocked._mockModel = model;
147
+ };
107
148
 
108
- public isA(sClassName: string): boolean {
149
+ // Default behavior
150
+ mocked.mock.isA.mockImplementation((sClassName: string) => {
109
151
  return sClassName === "sap.ui.model.odata.v4.ODataListBinding";
110
- }
111
-
112
- public setModel(oModel: MockModel) {
113
- this.mockModel = oModel;
114
- }
115
-
116
- // Mocked API
117
- public setAggregation = jest.fn();
118
- public filter = jest.fn();
119
- public sort = jest.fn();
120
-
121
- public getQueryOptionsFromParameters = jest.fn();
122
- public getPath = jest.fn();
123
- public isSuspended = jest.fn();
124
- public resume = jest.fn();
125
- public refresh = jest.fn();
126
- public suspend = jest.fn();
127
-
128
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
129
- public requestContexts = jest.fn((...args) => {
130
- return Promise.resolve(this.aMockContexts as any as Context[]);
131
152
  });
132
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
133
- public getCurrentContexts = jest.fn((...args) => {
134
- return this.aMockContexts as any as Context[];
153
+ mocked.mock.requestContexts.mockImplementation(() => {
154
+ return Promise.resolve(mocked._aMockContexts);
135
155
  });
136
- public getModel = jest.fn(() => {
137
- return this.mockModel as any as ODataModel;
156
+ mocked.mock.getCurrentContexts.mockImplementation(() => {
157
+ return mocked._aMockContexts;
138
158
  });
139
- public getUpdateGroupId = jest.fn(() => "auto");
140
- }
141
-
142
- export class MockPropertyBinding implements Partial<ODataPropertyBinding> {
143
- private mockModel!: JSONModel;
144
- private mockPath!: string;
159
+ mocked.mock.getAllCurrentContexts.mockImplementation(() => {
160
+ return mocked._aMockContexts;
161
+ });
162
+ mocked.mock.getModel.mockImplementation(() => {
163
+ return mocked._mockModel;
164
+ });
165
+ mocked.mock.getUpdateGroupId.mockReturnValue("auto");
145
166
 
146
- public constructor(mockModel: JSONModel, mockPath: string) {
147
- this.setModel(mockModel);
148
- this.setPath(mockPath);
149
- }
167
+ return mocked;
168
+ }
169
+ /**
170
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockListBinding instead.
171
+ */
172
+ export const MockListBinding: new (aContexts?: any[], mockModel?: MockModel) => MockListBinding = createMockListBinding as any;
150
173
 
151
- public isA(sClassName: string): boolean {
174
+ /**
175
+ * Utility type to mock a sap.ui.model.odata.v4.ODataPropertyBinding
176
+ */
177
+ export type MockPropertyBinding = WithMock<ODataPropertyBinding> & {
178
+ _value?: any;
179
+ _path?: string;
180
+ _mockModel?: MockModel;
181
+ };
182
+ /**
183
+ * Factory function to create a new MockPropertyBinding.
184
+ *
185
+ * @param value The value returnd by the PropertyBinding
186
+ * @param path The path of the PropertyBinding
187
+ * @param oMockModel The model of the PropertyBinding
188
+ * @returns A new MockPropertyBinding
189
+ */
190
+ export function createMockPropertyBinding(value: any, path?: string, oMockModel?: MockModel): MockPropertyBinding {
191
+ const mocked = mock(ODataPropertyBinding) as MockPropertyBinding;
192
+ mocked._mockModel = oMockModel;
193
+ mocked._value = value;
194
+ mocked._path = path;
195
+
196
+ // Default behavior
197
+ mocked.mock.isA.mockImplementation((sClassName: string) => {
152
198
  return sClassName === "sap/ui/model/odata/v4/ODataPropertyBinding";
153
- }
154
-
155
- public setModel(oModel: JSONModel) {
156
- this.mockModel = oModel;
157
- }
158
- public getModel = jest.fn(() => {
159
- return this.mockModel as any as ODataModel;
160
199
  });
161
-
162
- public setPath(path: string | undefined) {
163
- this.mockPath = path ? path : "";
164
- }
165
- public getPath = jest.fn(() => {
166
- return this.mockPath;
200
+ mocked.mock.getModel.mockImplementation(() => {
201
+ return mocked._mockModel;
167
202
  });
168
-
169
- public getValue = jest.fn(() => {
170
- return this?.mockModel?.getProperty(`/${this.mockPath}`);
203
+ mocked.mock.getValue.mockImplementation(() => {
204
+ return mocked._value;
205
+ });
206
+ mocked.mock.getPath.mockImplementation(() => {
207
+ return mocked._path;
171
208
  });
172
- }
173
209
 
174
- export class MockCompositeBinding implements Partial<CompositeBinding> {
175
- private aBindings: MockPropertyBinding[];
210
+ return mocked;
211
+ }
212
+ /**
213
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockPropertyBinding instead.
214
+ */
215
+ export const MockPropertyBinding: new (value: any, oMockModel?: MockModel) => MockPropertyBinding = createMockPropertyBinding as any;
176
216
 
177
- public constructor(aBindings: MockPropertyBinding[]) {
178
- this.aBindings = aBindings;
179
- }
217
+ /**
218
+ * Utility type to mock a sap.ui.model.CompositeBinding
219
+ */
220
+ export type MockCompositeBinding = WithMock<CompositeBinding> & {
221
+ _aBindings: MockPropertyBinding[];
222
+ };
223
+ /**
224
+ * Factory function to create a new MockCompositeBinding.
225
+ *
226
+ * @param aBindings The bindings of the CompositeBinding
227
+ * @returns A new MockCompositeBinding
228
+ */
229
+ export function createMockCompositeBinding(aBindings: MockPropertyBinding[]): MockCompositeBinding {
230
+ const mocked = mock(CompositeBinding) as MockCompositeBinding;
231
+ mocked._aBindings = aBindings;
180
232
 
181
- public isA(sClassName: string): boolean {
233
+ // Default behavior
234
+ mocked.mock.isA.mockImplementation((sClassName: string) => {
182
235
  return sClassName === "sap.ui.model.CompositeBinding";
183
- }
184
-
185
- public getBindings = jest.fn(() => {
186
- return this.aBindings;
187
236
  });
188
-
189
- public getValue = jest.fn(() => {
190
- return this.aBindings.map((binding) => binding.getValue());
237
+ mocked.mock.getBindings.mockImplementation(() => {
238
+ return mocked._aBindings;
239
+ });
240
+ mocked.mock.getValue.mockImplementation(() => {
241
+ return mocked._aBindings.map((binding) => binding.getValue());
191
242
  });
192
- }
193
243
 
194
- export class MockContextBinding implements Partial<ODataContextBinding> {
195
- private oMockContext: MockContext;
196
- private isKeptAlive: boolean = false;
244
+ return mocked;
245
+ }
246
+ /**
247
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockCompositeBinding instead.
248
+ */
249
+ export const MockCompositeBinding: new (aBindings: MockPropertyBinding[]) => MockCompositeBinding = createMockCompositeBinding as any;
197
250
 
198
- public constructor(oContext?: any, private mockModel?: MockModel) {
199
- this.oMockContext = new MockContext(oContext || {}, this);
200
- }
251
+ /**
252
+ * Utility type to mock a sap.ui.model.odata.v4.ODataContextBinding
253
+ */
254
+ export type MockContextBinding = WithMock<ODataContextBinding> & {
255
+ oMockContext: MockContext;
256
+ isKeptAlive: boolean;
257
+ mockModel?: MockModel;
258
+
259
+ /**
260
+ * Utility method to access the internal MockContext of the ContextBinding
261
+ */
262
+ getInternalMockContext: () => MockContext;
263
+ /**
264
+ * Utility method to set the model of the ContextBinding
265
+ */
266
+ setModel: (oModel: MockModel) => void;
267
+ };
268
+ /**
269
+ * Factory function to create a new MockContextBinding.
270
+ *
271
+ * @param oContext The context of the ContextBinding
272
+ * @param oMockModel The model of the ContextBinding
273
+ * @returns A new MockContextBinding
274
+ */
275
+ export function createMockContextBinding(oContext?: any, oMockModel?: MockModel): MockContextBinding {
276
+ const mocked = mock(ODataContextBinding) as MockContextBinding;
277
+ mocked.mockModel = oMockModel;
278
+ mocked.oMockContext = createMockContext(oContext || {}, mocked);
279
+
280
+ // Utility API
281
+ mocked.getInternalMockContext = () => {
282
+ return mocked.oMockContext;
283
+ };
284
+ mocked.setModel = (oModel: MockModel) => {
285
+ mocked.mockModel = oModel;
286
+ };
201
287
 
202
- public isA(sClassName: string): boolean {
288
+ // Default behavior
289
+ mocked.mock.isA.mockImplementation((sClassName: string) => {
203
290
  return sClassName === "sap.ui.model.odata.v4.ODataContextBinding";
204
- }
205
- public getInternalMockContext(): MockContext {
206
- return this.oMockContext;
207
- }
208
- public setModel(oModel: MockModel) {
209
- this.mockModel = oModel;
210
- }
211
-
212
- // Mocked API
213
- public getBoundContext = jest.fn(() => {
214
- return this.oMockContext as any as Context;
215
291
  });
216
- public attachEventOnce = jest.fn();
217
- public getModel = jest.fn(() => {
218
- return this.mockModel as any as ODataModel;
292
+ mocked.mock.getBoundContext.mockImplementation(() => {
293
+ return mocked.oMockContext;
219
294
  });
220
- public setParameter = jest.fn();
221
- public execute = jest.fn().mockResolvedValue(true);
222
- public getGroupId = jest.fn();
223
- }
295
+ mocked.mock.getModel.mockImplementation(() => {
296
+ return mocked.mockModel;
297
+ });
298
+ mocked.mock.execute.mockResolvedValue(true);
224
299
 
300
+ return mocked;
301
+ }
225
302
  /**
226
- * Mock class for OData V4 MetaModel
303
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockContextBinding instead.
227
304
  */
228
- export class MockMetaModel implements Partial<ODataMetaModel> {
229
- private oMetaContext: MockContext;
230
- requestValueListInfo!: jest.Mock<any, any>;
305
+ export const MockContextBinding: new (oContext?: any, oMockModel?: MockModel) => MockContextBinding = createMockContextBinding as any;
231
306
 
232
- public constructor(oMetaData?: any) {
233
- this.oMetaContext = new MockContext(oMetaData || {});
234
- }
307
+ /**
308
+ * Utility type to mock a sap.ui.model.odata.v4.ODataMetaModel
309
+ */
310
+ export type MockMetaModel = WithMock<ODataMetaModel> & {
311
+ oMetaContext: MockContext;
312
+ };
313
+ /**
314
+ * Factory function to create a new MockMetaModel.
315
+ *
316
+ * @param oMetaData A map of the different metadata properties of the MetaModel (path -> value).
317
+ * @returns A new MockMetaModel
318
+ */
319
+ export function createMockMetaModel(oMetaData?: any): MockMetaModel {
320
+ const mocked = mock(ODataMetaModel) as MockMetaModel;
321
+ mocked.oMetaContext = createMockContext(oMetaData || {});
235
322
 
236
- // Mocked API
237
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
238
- public getMetaContext = jest.fn((sPath: string) => {
239
- return new MockContext({ $path: sPath }) as any as Context;
323
+ // Default behavior
324
+ mocked.mock.getMetaContext.mockImplementation((sPath: string) => {
325
+ return createMockContext({ $path: sPath });
240
326
  });
241
- public getObject = jest.fn((sPath: string) => {
242
- return this.oMetaContext.getProperty(sPath);
327
+ mocked.mock.getObject.mockImplementation((sPath: string) => {
328
+ return mocked.oMetaContext.getProperty(sPath);
243
329
  });
244
- public requestObject = jest.fn();
245
- public createBindingContext = jest.fn((sPath: string) => {
246
- return new MockContext({ $path: sPath }) as any as Context;
330
+ mocked.mock.createBindingContext.mockImplementation((sPath: string) => {
331
+ return createMockContext({ $path: sPath });
247
332
  });
248
- public getMetaPath = jest.fn((sPath: string) => {
333
+ mocked.mock.getMetaPath.mockImplementation((sPath: string) => {
249
334
  const metamodel = new ODataMetaModel();
250
335
  return sPath ? metamodel.getMetaPath(sPath) : sPath;
251
336
  });
252
- public getContext = jest.fn();
253
- }
254
337
 
338
+ return mocked;
339
+ }
255
340
  /**
256
- * Mock class for OData V4 Model
341
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockMetaModel instead.
257
342
  */
343
+ export const MockMetaModel: new (oMetaData?: any) => MockMetaModel = createMockMetaModel as any;
258
344
 
259
- export class MockModel implements Partial<ODataModel> {
260
- private oMetaModel?: MockMetaModel;
261
-
262
- public constructor(private mockListBinding?: MockListBinding, private mockContextBinding?: MockContextBinding) {
263
- if (mockListBinding) {
264
- mockListBinding.setModel(this);
265
- }
266
- if (mockContextBinding) {
267
- mockContextBinding.setModel(this);
268
- }
269
- }
270
-
271
- // Factories
272
- static modelFromListBinding(mockListBinding: MockListBinding): MockModel {
273
- return new MockModel(mockListBinding);
345
+ /**
346
+ * Utility type to mock a sap.ui.model.odata.v4.ODataModel
347
+ */
348
+ export type MockModel = WithMock<ODataModel> & {
349
+ oMetaModel?: MockMetaModel;
350
+ mockListBinding?: MockListBinding;
351
+ mockContextBinding?: MockContextBinding;
352
+
353
+ /**
354
+ * Utility method to set the metamodel of the MockModel
355
+ */
356
+ setMetaModel: (oMetaModel: MockMetaModel) => void;
357
+ };
358
+ /**
359
+ * Factory function to create a new MockModel.
360
+ *
361
+ * @param oMockListBinding A list binding that will be returned when calling bindList.
362
+ * @param oMockContextBinding A context binding that will be returned when calling bindContext.
363
+ * @returns A new MockModel
364
+ */
365
+ export function createMockModel(oMockListBinding?: MockListBinding, oMockContextBinding?: MockContextBinding): MockModel {
366
+ const mocked = mock(ODataModel) as MockModel;
367
+ mocked.mockListBinding = oMockListBinding;
368
+ mocked.mockContextBinding = oMockContextBinding;
369
+ if (oMockListBinding) {
370
+ oMockListBinding.setModel(mocked);
274
371
  }
275
- static modelFromContextBinding(mockContextBinding: MockContextBinding): MockModel {
276
- return new MockModel(undefined, mockContextBinding);
372
+ if (oMockContextBinding) {
373
+ oMockContextBinding.setModel(mocked);
277
374
  }
278
375
 
279
- public setMetaModel(oMetaModel: MockMetaModel) {
280
- this.oMetaModel = oMetaModel;
281
- }
376
+ // Utility API
377
+ mocked.setMetaModel = (oMetaModel: MockMetaModel) => {
378
+ mocked.oMetaModel = oMetaModel;
379
+ };
282
380
 
283
- // Mocked API
284
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
285
- public bindList = jest.fn((...args) => {
286
- return this.mockListBinding as any as ODataListBinding;
381
+ // Default behavior
382
+ mocked.mock.bindList.mockImplementation(() => {
383
+ return mocked.mockListBinding;
287
384
  });
288
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
289
- public bindContext = jest.fn((...args) => {
290
- return this.mockContextBinding as any as ODataContextBinding;
385
+ mocked.mock.bindContext.mockImplementation(() => {
386
+ return mocked.mockContextBinding;
291
387
  });
292
- public getMetaModel = jest.fn(() => {
293
- return this.oMetaModel as any as ODataMetaModel;
388
+ mocked.mock.getMetaModel.mockImplementation(() => {
389
+ return mocked.oMetaModel;
294
390
  });
295
391
 
296
- public getProperty = jest.fn();
297
- public setProperty = jest.fn();
298
- public requestObject = jest.fn();
299
- public getKeepAliveContext = jest.fn();
392
+ return mocked;
393
+ }
394
+ /**
395
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockModel instead.
396
+ */
397
+ export const MockModel: new (oMockListBinding?: MockListBinding, oMockContextBinding?: MockContextBinding) => MockModel =
398
+ createMockModel as any;
399
+ /**
400
+ * Factory function to create a new MockModel used with a listBinding.
401
+ *
402
+ * @param oMockListBinding A list binding that will be returned when calling bindList.
403
+ * @returns A new MockModel
404
+ */
405
+ export function createMockModelFromListBinding(oMockListBinding: MockListBinding): MockModel {
406
+ return createMockModel(oMockListBinding);
407
+ }
408
+ /**
409
+ * Factory function to create a new MockModel used with a contextBinding.
410
+ *
411
+ * @param oMockContextBinding A context binding that will be returned when calling bindContext.
412
+ * @returns A new MockModel
413
+ */
414
+ export function createMockModelFromContextBinding(oMockContextBinding: MockContextBinding): MockModel {
415
+ return createMockModel(undefined, oMockContextBinding);
300
416
  }
301
417
 
302
- export class MockView implements Partial<View> {
303
- public getController = jest.fn();
304
-
305
- public getBindingContext = jest.fn();
306
-
307
- public getModel = jest.fn();
308
-
309
- public getViewData = jest.fn();
418
+ /**
419
+ * Utility type to mock a sap.ui.core.mvc.View
420
+ */
421
+ export type MockView = WithMock<View>;
422
+ /**
423
+ * Factory function to create a new MockView.
424
+ *
425
+ * @returns A new MockView
426
+ */
427
+ export function createMockView(): MockView {
428
+ const mocked = mock(View);
310
429
 
311
- public isA(sClassName: string): boolean {
430
+ // Default behavior
431
+ mocked.mock.isA.mockImplementation((sClassName: string) => {
312
432
  return sClassName === "sap.ui.core.mvc.View";
313
- }
433
+ });
314
434
 
315
- public getId = jest.fn();
435
+ return mocked;
316
436
  }
437
+ /**
438
+ * For compatibility reasons, we keep a new operator. Use the factory function createMockView instead.
439
+ */
440
+ export const MockView: new () => MockView = createMockView as any;
317
441
 
318
- export class MockController implements Partial<Controller> {
319
- public getView = jest.fn().mockReturnValue(new MockView());
320
-
321
- public _routing = {
322
- navigateToTarget: jest.fn()
323
- };
324
-
325
- public _editFlow = {
326
- syncTask: jest.fn()
327
- };
328
-
329
- public _sideEffects = {
330
- handleFieldChange: jest.fn()
331
- };
332
-
333
- isA = jest.fn().mockReturnValue(false);
442
+ /**
443
+ * Utility type to mock a sap.fe.core.PageController
444
+ */
445
+ export type MockController = WithMock<Controller> & {
446
+ _routing: WithMock<InternalRouting>;
447
+ _editFlow: WithMock<InternalEditFlow>;
448
+ _sideEffects: WithMock<SideEffects>;
449
+ editFlow: WithMock<EditFlow>;
450
+ };
451
+ /**
452
+ * Factory function to create a new MockController.
453
+ *
454
+ * @returns A new MockController
455
+ */
456
+ export function createMockController(): MockController {
457
+ const mocked = mock(Controller) as MockController;
458
+ mocked._routing = mock(InternalRouting);
459
+ mocked._editFlow = mock(InternalEditFlow);
460
+ mocked._sideEffects = mock(SideEffects);
461
+ mocked.editFlow = mock(EditFlow);
462
+
463
+ // Default Behavior
464
+ mocked.mock.getView.mockReturnValue(createMockView());
465
+ mocked.mock.isA.mockReturnValue(false);
466
+
467
+ return mocked;
334
468
  }
469
+ /**
470
+ * For compatibility reasons, we keep a new operator. Use the factory function mockController instead.
471
+ */
472
+ export const MockController: new () => MockController = createMockController as any;
335
473
 
336
474
  export interface MVCMock {
337
475
  model: MockModel;
338
476
  view: MockView;
339
477
  controller: MockController;
340
478
  }
341
-
342
479
  /**
343
480
  * Generate model, view and controller mocks that refer to each other.
344
481
  *
@@ -346,13 +483,13 @@ export interface MVCMock {
346
483
  * @returns Mocked model, view and controller instances
347
484
  */
348
485
  export function mockMVC(existing?: Partial<MVCMock>): MVCMock {
349
- const model = existing?.model || new MockModel();
350
- const view = existing?.view || new MockView();
351
- const controller = existing?.controller || new MockController();
486
+ const model = existing?.model || createMockModel();
487
+ const view = existing?.view || createMockView();
488
+ const controller = existing?.controller || createMockController();
352
489
 
353
- view.getController.mockReturnValue(controller);
354
- view.getModel.mockReturnValue(model);
355
- controller.getView.mockReturnValue(view);
490
+ view.mock.getController.mockReturnValue(controller);
491
+ view.mock.getModel.mockReturnValue(model);
492
+ controller.mock.getView.mockReturnValue(view);
356
493
 
357
494
  return { model, view, controller };
358
495
  }