@midwayjs/mock 3.1.6 → 3.2.2

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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { create, close, createApp, createFunctionApp, createLightApp, } from './creator';
2
2
  export * from './client/index';
3
3
  export { transformFrameworkToConfiguration } from './utils';
4
+ export * from './mock';
4
5
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -24,4 +24,5 @@ Object.defineProperty(exports, "createLightApp", { enumerable: true, get: functi
24
24
  __exportStar(require("./client/index"), exports);
25
25
  var utils_1 = require("./utils");
26
26
  Object.defineProperty(exports, "transformFrameworkToConfiguration", { enumerable: true, get: function () { return utils_1.transformFrameworkToConfiguration; } });
27
+ __exportStar(require("./mock"), exports);
27
28
  //# sourceMappingURL=index.js.map
package/dist/mock.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { IMidwayApplication, IMidwayContext } from '@midwayjs/core';
2
+ export declare function mockSession(app: IMidwayApplication, key: string, value: string): void;
3
+ export declare function mockHeader(app: IMidwayApplication, headerKey: string, headerValue: string): void;
4
+ export declare function mockClassProperty(clzz: new (...args: any[]) => any, propertyName: string, value: any): void;
5
+ export declare function mockProperty(obj: any, key: string, value: any): void;
6
+ export declare function restoreAllMocks(): void;
7
+ export declare function mockContext(app: IMidwayApplication, key: string | ((ctx: IMidwayContext) => void), value?: PropertyDescriptor | any): void;
8
+ //# sourceMappingURL=mock.d.ts.map
package/dist/mock.js ADDED
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mockContext = exports.restoreAllMocks = exports.mockProperty = exports.mockClassProperty = exports.mockHeader = exports.mockSession = void 0;
4
+ const core_1 = require("@midwayjs/core");
5
+ function getMockService(app) {
6
+ if (!app) {
7
+ app = (0, core_1.getCurrentMainApp)();
8
+ }
9
+ if (!app.getApplicationContext) {
10
+ throw new Error('[mock]: app.getApplicationContext is undefined.');
11
+ }
12
+ const applicationContext = app.getApplicationContext();
13
+ const mockService = applicationContext.get(core_1.MidwayMockService);
14
+ if (!mockService) {
15
+ throw new Error('[mock]: MidwayMockService is undefined.');
16
+ }
17
+ return mockService;
18
+ }
19
+ function mockSession(app, key, value) {
20
+ const mockService = getMockService(app);
21
+ mockService.mockContext(app, (ctx) => {
22
+ if (!ctx.session) {
23
+ ctx.session = {};
24
+ }
25
+ ctx.session[key] = value;
26
+ });
27
+ }
28
+ exports.mockSession = mockSession;
29
+ function mockHeader(app, headerKey, headerValue) {
30
+ const mockService = getMockService(app);
31
+ mockService.mockContext(app, (ctx) => {
32
+ ctx.headers[headerKey] = headerValue;
33
+ });
34
+ }
35
+ exports.mockHeader = mockHeader;
36
+ function mockClassProperty(clzz, propertyName, value) {
37
+ const mockService = getMockService();
38
+ return mockService.mockClassProperty(clzz, propertyName, value);
39
+ }
40
+ exports.mockClassProperty = mockClassProperty;
41
+ function mockProperty(obj, key, value) {
42
+ const mockService = getMockService();
43
+ return mockService.mockProperty(obj, key, value);
44
+ }
45
+ exports.mockProperty = mockProperty;
46
+ function restoreAllMocks() {
47
+ const mockService = getMockService();
48
+ mockService.restore();
49
+ }
50
+ exports.restoreAllMocks = restoreAllMocks;
51
+ function mockContext(app, key, value) {
52
+ const mockService = getMockService(app);
53
+ mockService.mockContext(app, key, value);
54
+ }
55
+ exports.mockContext = mockContext;
56
+ //# sourceMappingURL=mock.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/mock",
3
- "version": "3.1.6",
3
+ "version": "3.2.2",
4
4
  "description": "create your test app from midway framework",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index.d.ts",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "license": "MIT",
28
28
  "devDependencies": {
29
- "@midwayjs/core": "^3.1.6",
29
+ "@midwayjs/core": "^3.2.2",
30
30
  "@midwayjs/decorator": "^3.1.6",
31
31
  "@midwayjs/logger": "^2.15.0",
32
32
  "@types/amqplib": "0.8.2",
@@ -45,5 +45,5 @@
45
45
  "type": "git",
46
46
  "url": "http://github.com/midwayjs/midway.git"
47
47
  },
48
- "gitHead": "8c157a2b1a23ca2360522dc0de58513098952391"
48
+ "gitHead": "6bc097035302d01ae901d76a10e080db8554a864"
49
49
  }