@portal-hq/web 3.5.2 → 3.5.3

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.
@@ -35,11 +35,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.ChainNamespace = exports.PortalCurve = exports.GetTransactionsOrder = exports.BackupMethods = exports.MpcStatuses = exports.RequestMethod = exports.MpcErrorCodes = exports.MpcError = void 0;
38
+ exports.ChainNamespace = exports.PortalCurve = exports.GetTransactionsOrder = exports.BackupMethods = exports.MpcStatuses = exports.RequestMethod = exports.PortalMpcError = exports.MpcErrorCodes = exports.MpcError = void 0;
39
39
  const web3_js_1 = require("@solana/web3.js");
40
40
  const mpc_1 = __importDefault(require("./mpc"));
41
41
  const provider_1 = __importStar(require("./provider"));
42
42
  const yield_1 = __importDefault(require("./integrations/yield"));
43
+ const trading_1 = __importDefault(require("./integrations/trading"));
43
44
  class Portal {
44
45
  get ready() {
45
46
  return this.mpc.ready;
@@ -83,6 +84,7 @@ class Portal {
83
84
  portal: this,
84
85
  });
85
86
  this.yield = new yield_1.default({ mpc: this.mpc });
87
+ this.trading = new trading_1.default({ mpc: this.mpc });
86
88
  this.provider = new provider_1.default({
87
89
  portal: this,
88
90
  chainId: chainId ? Number(chainId) : undefined,
@@ -703,12 +705,18 @@ class Portal {
703
705
  /*******************************
704
706
  * Swaps Methods
705
707
  *******************************/
708
+ /**
709
+ * @deprecated This method is deprecated. Use `portal.trading.zeroX.getQuote` instead.
710
+ */
706
711
  getQuote(apiKey, args, chainId) {
707
712
  var _a;
708
713
  return __awaiter(this, void 0, void 0, function* () {
709
714
  return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getQuote(apiKey, args, chainId);
710
715
  });
711
716
  }
717
+ /**
718
+ * @deprecated This method is deprecated. Use `portal.trading.zeroX.getSources` instead.
719
+ */
712
720
  getSources(apiKey, chainId) {
713
721
  var _a;
714
722
  return __awaiter(this, void 0, void 0, function* () {
@@ -804,6 +812,8 @@ class Portal {
804
812
  var mpc_2 = require("./mpc");
805
813
  Object.defineProperty(exports, "MpcError", { enumerable: true, get: function () { return mpc_2.MpcError; } });
806
814
  Object.defineProperty(exports, "MpcErrorCodes", { enumerable: true, get: function () { return mpc_2.MpcErrorCodes; } });
815
+ var errors_1 = require("./mpc/errors");
816
+ Object.defineProperty(exports, "PortalMpcError", { enumerable: true, get: function () { return errors_1.PortalMpcError; } });
807
817
  var provider_2 = require("./provider");
808
818
  Object.defineProperty(exports, "RequestMethod", { enumerable: true, get: function () { return provider_2.RequestMethod; } });
809
819
  var MpcStatuses;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const zero_x_1 = __importDefault(require("./zero-x"));
7
+ const lifi_1 = __importDefault(require("./lifi"));
8
+ /**
9
+ * This class is a container for the LiFi class.
10
+ * In the future, Trading domain logic should be here.
11
+ */
12
+ class Trading {
13
+ constructor({ mpc }) {
14
+ this.lifi = new lifi_1.default({ mpc });
15
+ this.zeroX = new zero_x_1.default({ mpc });
16
+ }
17
+ }
18
+ exports.default = Trading;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ class LiFi {
13
+ constructor({ mpc }) {
14
+ this.mpc = mpc;
15
+ }
16
+ /**
17
+ * Retrieves routes from the Li.Fi integration.
18
+ * @param data - The parameters for the routes request.
19
+ * @returns A `LifiRoutesResponse` promise.
20
+ * @throws An error if the operation fails.
21
+ */
22
+ getRoutes(data) {
23
+ var _a;
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getLifiRoutes(data);
26
+ });
27
+ }
28
+ /**
29
+ * Retrieves a quote from the Li.Fi integration.
30
+ * @param data - The parameters for the quote request.
31
+ * @returns A `LifiQuoteResponse` promise.
32
+ * @throws An error if the operation fails.
33
+ */
34
+ getQuote(data) {
35
+ var _a;
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getLifiQuote(data);
38
+ });
39
+ }
40
+ /**
41
+ * Retrieves the status of a transaction from the Li.Fi integration.
42
+ * @param data - The parameters for the status request.
43
+ * @returns A `LifiStatusResponse` promise.
44
+ * @throws An error if the operation fails.
45
+ */
46
+ getStatus(data) {
47
+ var _a;
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getLifiStatus(data);
50
+ });
51
+ }
52
+ /**
53
+ * Retrieves an unsigned transaction from the Li.Fi integration that has yet to be signed/submitted.
54
+ * @param data - The step transaction request containing the step details.
55
+ * @returns A `LifiStepTransactionResponse` promise.
56
+ * @throws An error if the operation fails.
57
+ */
58
+ getRouteStep(data) {
59
+ var _a;
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getLifiRouteStep(data);
62
+ });
63
+ }
64
+ }
65
+ exports.default = LiFi;
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ /**
3
+ * @jest-environment jsdom
4
+ */
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const _1 = __importDefault(require("."));
19
+ const mpc_1 = __importDefault(require("../../../mpc"));
20
+ const portal_1 = __importDefault(require("../../../__mocks/portal/portal"));
21
+ const constants_1 = require("../../../__mocks/constants");
22
+ describe('LiFi', () => {
23
+ let lifi;
24
+ let mpc;
25
+ beforeEach(() => {
26
+ jest.clearAllMocks();
27
+ portal_1.default.host = constants_1.mockHost;
28
+ mpc = new mpc_1.default({
29
+ portal: portal_1.default,
30
+ });
31
+ lifi = new _1.default({ mpc });
32
+ });
33
+ describe('getRoutes', () => {
34
+ it('should call mpc.getLifiRoutes with the correct arguments', () => __awaiter(void 0, void 0, void 0, function* () {
35
+ const spy = jest
36
+ .spyOn(mpc, 'getLifiRoutes')
37
+ .mockResolvedValue(constants_1.mockLifiGetRoutesResponse);
38
+ const result = yield lifi.getRoutes(constants_1.mockLifiGetRoutesRequest);
39
+ expect(spy).toHaveBeenCalledWith(constants_1.mockLifiGetRoutesRequest);
40
+ expect(result).toEqual(constants_1.mockLifiGetRoutesResponse);
41
+ }));
42
+ it('should propagate errors from mpc.getLifiRoutes', () => __awaiter(void 0, void 0, void 0, function* () {
43
+ const error = new Error('Test error');
44
+ jest.spyOn(mpc, 'getLifiRoutes').mockRejectedValue(error);
45
+ yield expect(lifi.getRoutes(constants_1.mockLifiGetRoutesRequest)).rejects.toThrow('Test error');
46
+ }));
47
+ });
48
+ describe('getQuote', () => {
49
+ it('should call mpc.getLifiQuote with the correct arguments', () => __awaiter(void 0, void 0, void 0, function* () {
50
+ const spy = jest
51
+ .spyOn(mpc, 'getLifiQuote')
52
+ .mockResolvedValue(constants_1.mockLifiGetQuoteResponse);
53
+ const result = yield lifi.getQuote(constants_1.mockLifiGetQuoteRequest);
54
+ expect(spy).toHaveBeenCalledWith(constants_1.mockLifiGetQuoteRequest);
55
+ expect(result).toEqual(constants_1.mockLifiGetQuoteResponse);
56
+ }));
57
+ it('should propagate errors from mpc.getLifiQuote', () => __awaiter(void 0, void 0, void 0, function* () {
58
+ const error = new Error('Test error');
59
+ jest.spyOn(mpc, 'getLifiQuote').mockRejectedValue(error);
60
+ yield expect(lifi.getQuote(constants_1.mockLifiGetQuoteRequest)).rejects.toThrow('Test error');
61
+ }));
62
+ });
63
+ describe('getStatus', () => {
64
+ it('should call mpc.getLifiStatus with the correct arguments', () => __awaiter(void 0, void 0, void 0, function* () {
65
+ const spy = jest
66
+ .spyOn(mpc, 'getLifiStatus')
67
+ .mockResolvedValue(constants_1.mockLifiGetStatusResponse);
68
+ const result = yield lifi.getStatus(constants_1.mockLifiGetStatusRequest);
69
+ expect(spy).toHaveBeenCalledWith(constants_1.mockLifiGetStatusRequest);
70
+ expect(result).toEqual(constants_1.mockLifiGetStatusResponse);
71
+ }));
72
+ it('should propagate errors from mpc.getLifiStatus', () => __awaiter(void 0, void 0, void 0, function* () {
73
+ const error = new Error('Test error');
74
+ jest.spyOn(mpc, 'getLifiStatus').mockRejectedValue(error);
75
+ yield expect(lifi.getStatus(constants_1.mockLifiGetStatusRequest)).rejects.toThrow('Test error');
76
+ }));
77
+ });
78
+ describe('getRouteStep', () => {
79
+ it('should call mpc.getLifiRouteStep with the correct arguments', () => __awaiter(void 0, void 0, void 0, function* () {
80
+ const spy = jest
81
+ .spyOn(mpc, 'getLifiRouteStep')
82
+ .mockResolvedValue(constants_1.mockLifiGetRouteStepResponse);
83
+ const result = yield lifi.getRouteStep(constants_1.mockLifiGetRouteStepRequest);
84
+ expect(spy).toHaveBeenCalledWith(constants_1.mockLifiGetRouteStepRequest);
85
+ expect(result).toEqual(constants_1.mockLifiGetRouteStepResponse);
86
+ }));
87
+ it('should propagate errors from mpc.getLifiRouteStep', () => __awaiter(void 0, void 0, void 0, function* () {
88
+ const error = new Error('Test error');
89
+ jest.spyOn(mpc, 'getLifiRouteStep').mockRejectedValue(error);
90
+ yield expect(lifi.getRouteStep(constants_1.mockLifiGetRouteStepRequest)).rejects.toThrow('Test error');
91
+ }));
92
+ });
93
+ });
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ class ZeroX {
13
+ constructor({ mpc }) {
14
+ this.mpc = mpc;
15
+ }
16
+ /**
17
+ * Get a quote from the Swaps API.
18
+ *
19
+ * @param apiKey - The API key for the Swaps API.
20
+ * @param args - The arguments for the quote.
21
+ * @param chainId - The chain ID for the quote.
22
+ * @returns The quote response.
23
+ */
24
+ getQuote(apiKey, args, chainId) {
25
+ var _a;
26
+ return __awaiter(this, void 0, void 0, function* () {
27
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getQuote(apiKey, args, chainId);
28
+ });
29
+ }
30
+ /**
31
+ * Get the valid, swappable token sources that can be used with your Portal MPC Wallet.
32
+ *
33
+ * @param apiKey - The API key for the Swaps API.
34
+ * @param chainId - The chain ID for the sources.
35
+ * @returns The sources response.
36
+ */
37
+ getSources(apiKey, chainId) {
38
+ var _a;
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ return (_a = this.mpc) === null || _a === void 0 ? void 0 : _a.getSources(apiKey, chainId);
41
+ });
42
+ }
43
+ }
44
+ exports.default = ZeroX;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ /**
3
+ * @jest-environment jsdom
4
+ */
5
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
+ return new (P || (P = Promise))(function (resolve, reject) {
8
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
12
+ });
13
+ };
14
+ var __importDefault = (this && this.__importDefault) || function (mod) {
15
+ return (mod && mod.__esModule) ? mod : { "default": mod };
16
+ };
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ const _1 = __importDefault(require("."));
19
+ const mpc_1 = __importDefault(require("../../../mpc"));
20
+ const portal_1 = __importDefault(require("../../../__mocks/portal/portal"));
21
+ const constants_1 = require("../../../__mocks/constants");
22
+ describe('ZeroX', () => {
23
+ let zeroX;
24
+ let mpc;
25
+ beforeEach(() => {
26
+ jest.clearAllMocks();
27
+ portal_1.default.host = constants_1.mockHost;
28
+ mpc = new mpc_1.default({
29
+ portal: portal_1.default,
30
+ });
31
+ zeroX = new _1.default({ mpc });
32
+ });
33
+ describe('getQuote', () => {
34
+ it('should correctly call mpc.getQuote', () => __awaiter(void 0, void 0, void 0, function* () {
35
+ const spy = jest
36
+ .spyOn(mpc, 'getQuote')
37
+ .mockResolvedValue(constants_1.mockZeroXQuoteResponse);
38
+ const result = yield zeroX.getQuote('test', constants_1.mockQuoteArgs, 'eip155:1');
39
+ expect(spy).toHaveBeenCalledTimes(1);
40
+ expect(spy).toHaveBeenCalledWith('test', constants_1.mockQuoteArgs, 'eip155:1');
41
+ expect(result).toEqual(constants_1.mockZeroXQuoteResponse);
42
+ }));
43
+ it('should propagate errors from mpc.getQuote', () => __awaiter(void 0, void 0, void 0, function* () {
44
+ const error = new Error('Test error');
45
+ jest.spyOn(mpc, 'getQuote').mockRejectedValue(error);
46
+ yield expect(zeroX.getQuote('test', constants_1.mockQuoteArgs, 'eip155:1')).rejects.toThrow('Test error');
47
+ }));
48
+ });
49
+ describe('getSources', () => {
50
+ it('should correctly call mpc.getSources', () => __awaiter(void 0, void 0, void 0, function* () {
51
+ const spy = jest
52
+ .spyOn(mpc, 'getSources')
53
+ .mockResolvedValue(constants_1.mockSourcesRes);
54
+ const result = yield zeroX.getSources('test', 'eip155:1');
55
+ expect(spy).toHaveBeenCalledTimes(1);
56
+ expect(spy).toHaveBeenCalledWith('test', 'eip155:1');
57
+ expect(result).toEqual(constants_1.mockSourcesRes);
58
+ }));
59
+ it('should propagate errors from mpc.getSources', () => __awaiter(void 0, void 0, void 0, function* () {
60
+ const error = new Error('Test error');
61
+ jest.spyOn(mpc, 'getSources').mockRejectedValue(error);
62
+ yield expect(zeroX.getSources('test', 'eip155:1')).rejects.toThrow('Test error');
63
+ }));
64
+ });
65
+ });
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MpcErrorCodes = exports.MpcError = void 0;
13
13
  const errors_1 = require("./errors");
14
14
  const index_1 = require("../index");
15
- const WEB_SDK_VERSION = '3.5.2';
15
+ const WEB_SDK_VERSION = '3.5.3';
16
16
  class Mpc {
17
17
  get ready() {
18
18
  return this._ready;
@@ -1072,6 +1072,46 @@ class Mpc {
1072
1072
  });
1073
1073
  });
1074
1074
  }
1075
+ getLifiRoutes(data) {
1076
+ return __awaiter(this, void 0, void 0, function* () {
1077
+ return this.handleRequestToIframeAndPost({
1078
+ methodMessage: 'portal:lifi:getRoutes',
1079
+ errorMessage: 'portal:lifi:getRoutesError',
1080
+ resultMessage: 'portal:lifi:getRoutesResult',
1081
+ data,
1082
+ });
1083
+ });
1084
+ }
1085
+ getLifiQuote(data) {
1086
+ return __awaiter(this, void 0, void 0, function* () {
1087
+ return this.handleRequestToIframeAndPost({
1088
+ methodMessage: 'portal:lifi:getQuote',
1089
+ errorMessage: 'portal:lifi:getQuoteError',
1090
+ resultMessage: 'portal:lifi:getQuoteResult',
1091
+ data,
1092
+ });
1093
+ });
1094
+ }
1095
+ getLifiStatus(data) {
1096
+ return __awaiter(this, void 0, void 0, function* () {
1097
+ return this.handleRequestToIframeAndPost({
1098
+ methodMessage: 'portal:lifi:getStatus',
1099
+ errorMessage: 'portal:lifi:getStatusError',
1100
+ resultMessage: 'portal:lifi:getStatusResult',
1101
+ data,
1102
+ });
1103
+ });
1104
+ }
1105
+ getLifiRouteStep(data) {
1106
+ return __awaiter(this, void 0, void 0, function* () {
1107
+ return this.handleRequestToIframeAndPost({
1108
+ methodMessage: 'portal:lifi:getRouteStep',
1109
+ errorMessage: 'portal:lifi:getRouteStepError',
1110
+ resultMessage: 'portal:lifi:getRouteStepResult',
1111
+ data,
1112
+ });
1113
+ });
1114
+ }
1075
1115
  /***************************
1076
1116
  * Private Methods
1077
1117
  ***************************/
@@ -2238,4 +2238,264 @@ describe('Mpc', () => {
2238
2238
  });
2239
2239
  });
2240
2240
  });
2241
+ describe('getLifiRoutes', () => {
2242
+ const args = constants_1.mockLifiGetRoutesRequest;
2243
+ const res = constants_1.mockLifiGetRoutesResponse;
2244
+ it('should successfully return the routes', (done) => {
2245
+ var _a;
2246
+ jest
2247
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2248
+ .mockImplementation((message, origin) => {
2249
+ const { type, data } = message;
2250
+ expect(type).toEqual('portal:lifi:getRoutes');
2251
+ expect(data).toEqual(args);
2252
+ expect(origin).toEqual(mockHostOrigin);
2253
+ window.dispatchEvent(new MessageEvent('message', {
2254
+ origin: mockHostOrigin,
2255
+ data: {
2256
+ type: 'portal:lifi:getRoutesResult',
2257
+ data: res,
2258
+ },
2259
+ }));
2260
+ });
2261
+ mpc
2262
+ .getLifiRoutes(args)
2263
+ .then((data) => {
2264
+ expect(data).toEqual(res);
2265
+ done();
2266
+ })
2267
+ .catch((_) => {
2268
+ expect(0).toEqual(1);
2269
+ done();
2270
+ });
2271
+ });
2272
+ it('should error out if the iframe sends an error message', (done) => {
2273
+ var _a;
2274
+ jest
2275
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2276
+ .mockImplementationOnce((message, origin) => {
2277
+ const { type, data } = message;
2278
+ expect(type).toEqual('portal:lifi:getRoutes');
2279
+ expect(data).toEqual(args);
2280
+ expect(origin).toEqual(mockHostOrigin);
2281
+ window.dispatchEvent(new MessageEvent('message', {
2282
+ origin: mockHostOrigin,
2283
+ data: {
2284
+ type: 'portal:lifi:getRoutesError',
2285
+ data: {
2286
+ code: 1,
2287
+ message: 'test',
2288
+ },
2289
+ },
2290
+ }));
2291
+ });
2292
+ mpc
2293
+ .getLifiRoutes(args)
2294
+ .then(() => {
2295
+ expect(0).toEqual(1);
2296
+ done();
2297
+ })
2298
+ .catch((e) => {
2299
+ expect(e).toBeInstanceOf(errors_1.PortalMpcError);
2300
+ expect(e.message).toEqual('test');
2301
+ expect(e.code).toEqual(1);
2302
+ done();
2303
+ });
2304
+ });
2305
+ });
2306
+ describe('getLifiQuote', () => {
2307
+ const args = constants_1.mockLifiGetQuoteRequest;
2308
+ const res = constants_1.mockLifiGetQuoteResponse;
2309
+ it('should successfully return the quote', (done) => {
2310
+ var _a;
2311
+ jest
2312
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2313
+ .mockImplementation((message, origin) => {
2314
+ const { type, data } = message;
2315
+ expect(type).toEqual('portal:lifi:getQuote');
2316
+ expect(data).toEqual(args);
2317
+ expect(origin).toEqual(mockHostOrigin);
2318
+ window.dispatchEvent(new MessageEvent('message', {
2319
+ origin: mockHostOrigin,
2320
+ data: {
2321
+ type: 'portal:lifi:getQuoteResult',
2322
+ data: res,
2323
+ },
2324
+ }));
2325
+ });
2326
+ mpc
2327
+ .getLifiQuote(args)
2328
+ .then((data) => {
2329
+ expect(data).toEqual(res);
2330
+ done();
2331
+ })
2332
+ .catch((_) => {
2333
+ expect(0).toEqual(1);
2334
+ done();
2335
+ });
2336
+ });
2337
+ it('should error out if the iframe sends an error message', (done) => {
2338
+ var _a;
2339
+ jest
2340
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2341
+ .mockImplementationOnce((message, origin) => {
2342
+ const { type, data } = message;
2343
+ expect(type).toEqual('portal:lifi:getQuote');
2344
+ expect(data).toEqual(args);
2345
+ expect(origin).toEqual(mockHostOrigin);
2346
+ window.dispatchEvent(new MessageEvent('message', {
2347
+ origin: mockHostOrigin,
2348
+ data: {
2349
+ type: 'portal:lifi:getQuoteError',
2350
+ data: {
2351
+ code: 1,
2352
+ message: 'test',
2353
+ },
2354
+ },
2355
+ }));
2356
+ });
2357
+ mpc
2358
+ .getLifiQuote(args)
2359
+ .then(() => {
2360
+ expect(0).toEqual(1);
2361
+ done();
2362
+ })
2363
+ .catch((e) => {
2364
+ expect(e).toBeInstanceOf(errors_1.PortalMpcError);
2365
+ expect(e.message).toEqual('test');
2366
+ expect(e.code).toEqual(1);
2367
+ done();
2368
+ });
2369
+ });
2370
+ });
2371
+ describe('getLifiStatus', () => {
2372
+ const args = constants_1.mockLifiGetStatusRequest;
2373
+ const res = constants_1.mockLifiGetStatusResponse;
2374
+ it('should successfully return the status', (done) => {
2375
+ var _a;
2376
+ jest
2377
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2378
+ .mockImplementation((message, origin) => {
2379
+ const { type, data } = message;
2380
+ expect(type).toEqual('portal:lifi:getStatus');
2381
+ expect(data).toEqual(args);
2382
+ expect(origin).toEqual(mockHostOrigin);
2383
+ window.dispatchEvent(new MessageEvent('message', {
2384
+ origin: mockHostOrigin,
2385
+ data: {
2386
+ type: 'portal:lifi:getStatusResult',
2387
+ data: res,
2388
+ },
2389
+ }));
2390
+ });
2391
+ mpc
2392
+ .getLifiStatus(args)
2393
+ .then((data) => {
2394
+ expect(data).toEqual(res);
2395
+ done();
2396
+ })
2397
+ .catch((_) => {
2398
+ expect(0).toEqual(1);
2399
+ done();
2400
+ });
2401
+ });
2402
+ it('should error out if the iframe sends an error message', (done) => {
2403
+ var _a;
2404
+ jest
2405
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2406
+ .mockImplementationOnce((message, origin) => {
2407
+ const { type, data } = message;
2408
+ expect(type).toEqual('portal:lifi:getStatus');
2409
+ expect(data).toEqual(args);
2410
+ expect(origin).toEqual(mockHostOrigin);
2411
+ window.dispatchEvent(new MessageEvent('message', {
2412
+ origin: mockHostOrigin,
2413
+ data: {
2414
+ type: 'portal:lifi:getStatusError',
2415
+ data: {
2416
+ code: 1,
2417
+ message: 'test',
2418
+ },
2419
+ },
2420
+ }));
2421
+ });
2422
+ mpc
2423
+ .getLifiStatus(args)
2424
+ .then(() => {
2425
+ expect(0).toEqual(1);
2426
+ done();
2427
+ })
2428
+ .catch((e) => {
2429
+ expect(e).toBeInstanceOf(errors_1.PortalMpcError);
2430
+ expect(e.message).toEqual('test');
2431
+ expect(e.code).toEqual(1);
2432
+ done();
2433
+ });
2434
+ });
2435
+ });
2436
+ describe('getLifiRouteStep', () => {
2437
+ const args = constants_1.mockLifiGetRouteStepRequest;
2438
+ const res = constants_1.mockLifiGetRouteStepResponse;
2439
+ it('should successfully return the route step', (done) => {
2440
+ var _a;
2441
+ jest
2442
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2443
+ .mockImplementation((message, origin) => {
2444
+ const { type, data } = message;
2445
+ expect(type).toEqual('portal:lifi:getRouteStep');
2446
+ expect(data).toEqual(args);
2447
+ expect(origin).toEqual(mockHostOrigin);
2448
+ window.dispatchEvent(new MessageEvent('message', {
2449
+ origin: mockHostOrigin,
2450
+ data: {
2451
+ type: 'portal:lifi:getRouteStepResult',
2452
+ data: res,
2453
+ },
2454
+ }));
2455
+ });
2456
+ mpc
2457
+ .getLifiRouteStep(args)
2458
+ .then((data) => {
2459
+ expect(data).toEqual(res);
2460
+ done();
2461
+ })
2462
+ .catch((_) => {
2463
+ expect(0).toEqual(1);
2464
+ done();
2465
+ });
2466
+ });
2467
+ it('should error out if the iframe sends an error message', (done) => {
2468
+ var _a;
2469
+ jest
2470
+ .spyOn((_a = mpc.iframe) === null || _a === void 0 ? void 0 : _a.contentWindow, 'postMessage')
2471
+ .mockImplementationOnce((message, origin) => {
2472
+ const { type, data } = message;
2473
+ expect(type).toEqual('portal:lifi:getRouteStep');
2474
+ expect(data).toEqual(args);
2475
+ expect(origin).toEqual(mockHostOrigin);
2476
+ window.dispatchEvent(new MessageEvent('message', {
2477
+ origin: mockHostOrigin,
2478
+ data: {
2479
+ type: 'portal:lifi:getRouteStepError',
2480
+ data: {
2481
+ code: 1,
2482
+ message: 'test',
2483
+ },
2484
+ },
2485
+ }));
2486
+ });
2487
+ mpc
2488
+ .getLifiRouteStep(args)
2489
+ .then(() => {
2490
+ expect(0).toEqual(1);
2491
+ done();
2492
+ })
2493
+ .catch((e) => {
2494
+ expect(e).toBeInstanceOf(errors_1.PortalMpcError);
2495
+ expect(e.message).toEqual('test');
2496
+ expect(e.code).toEqual(1);
2497
+ done();
2498
+ });
2499
+ });
2500
+ });
2241
2501
  });