@sprucelabs/spruce-heartwood-utils 7.5.0 → 7.7.0

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,2 +1,3 @@
1
1
  export { default as RemoteViewControllerFactory } from './viewControllers/RemoteViewControllerFactory';
2
2
  export { default as CardRegistrar } from './viewControllers/CardRegistrar';
3
+ export { default as remoteVcAssert } from './tests/remoteVcAssert';
@@ -1,2 +1,3 @@
1
1
  export { default as RemoteViewControllerFactory } from './viewControllers/RemoteViewControllerFactory.js';
2
2
  export { default as CardRegistrar } from './viewControllers/CardRegistrar.js';
3
+ export { default as remoteVcAssert } from './tests/remoteVcAssert.js';
@@ -0,0 +1,21 @@
1
+ import { SkillViewController } from '@sprucelabs/heartwood-view-controllers';
2
+ import { SkillEventContract } from '@sprucelabs/mercury-types';
3
+ import { ArgsFromSvc, ViewFixture } from '@sprucelabs/spruce-test-fixtures';
4
+ declare type EventNames = keyof SkillEventContract['eventSignatures'];
5
+ export interface AssertRendersRemoteCardsOptions<Svc extends SkillViewController = SkillViewController> {
6
+ svc: Svc;
7
+ fqen: EventNames;
8
+ views: ViewFixture;
9
+ expectedTarget?: Record<string, any>;
10
+ expectedPayload?: Record<string, any>;
11
+ loadArgs?: ArgsFromSvc<Svc>;
12
+ shouldInvoke?: {
13
+ methodName: string;
14
+ expectedParams?: any[];
15
+ };
16
+ }
17
+ declare const remoteVcAssert: {
18
+ assertSkillViewRendersRemoteCards<Svc extends SkillViewController<Record<string, any>> = SkillViewController<Record<string, any>>>(options: AssertRendersRemoteCardsOptions<Svc>): Promise<void>;
19
+ };
20
+ export declare function generateId(): string;
21
+ export default remoteVcAssert;
@@ -0,0 +1,143 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { AbstractViewController, vcAssert, } from '@sprucelabs/heartwood-view-controllers';
11
+ import { assertOptions } from '@sprucelabs/schema';
12
+ import { eventFaker, } from '@sprucelabs/spruce-test-fixtures';
13
+ import { assert } from '@sprucelabs/test';
14
+ const remoteVcAssert = {
15
+ assertSkillViewRendersRemoteCards(options) {
16
+ var _a;
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ const { views, svc: svc, expectedTarget, expectedPayload, fqen, shouldInvoke, loadArgs, } = assertOptions(options, [
19
+ 'svc',
20
+ 'fqen',
21
+ 'views',
22
+ ]);
23
+ const factory = views.getFactory();
24
+ factory.setController('heartwood.error-card', ThrowingErrorCardVc);
25
+ const vc1Id = generateId();
26
+ const vc2Id = generateId();
27
+ let wasHit = false;
28
+ let passedTarget;
29
+ let passedPayload;
30
+ yield eventFaker.on(fqen,
31
+ //@ts-ignore
32
+ ({ target, payload }) => __awaiter(this, void 0, void 0, function* () {
33
+ passedTarget = target;
34
+ passedPayload = payload;
35
+ wasHit = true;
36
+ return {
37
+ vcIds: ['assert.' + vc1Id, 'assert.' + vc2Id],
38
+ };
39
+ }));
40
+ yield eventFaker.on('heartwood.get-skill-views::v2021_02_11', () => {
41
+ const source = [
42
+ generateVcSource({
43
+ vcId: vc1Id,
44
+ count: 1,
45
+ stubMethod: shouldInvoke === null || shouldInvoke === void 0 ? void 0 : shouldInvoke.methodName,
46
+ }),
47
+ generateVcSource({
48
+ vcId: vc2Id,
49
+ count: 2,
50
+ stubMethod: shouldInvoke === null || shouldInvoke === void 0 ? void 0 : shouldInvoke.methodName,
51
+ }),
52
+ `heartwood({ TestEventViewController1, TestEventViewController2 })`,
53
+ ].join('\n');
54
+ return {
55
+ id: generateId(),
56
+ ids: [vc1Id, vc2Id],
57
+ source,
58
+ };
59
+ });
60
+ yield views.load(svc, loadArgs);
61
+ assert.isTrue(wasHit, `You did not load any remote cards. Next step is to add the following to your skill view's load():\n\nconst registrar = new CardRegistrar(...)\nconst cards = await registrar.fetch(...)\n\nAlso, make sure you create the new event that you'll emit when trying to fetch remote cards, something like 'my-skill.register-cards::v2020_02_02'\n\n`);
62
+ if (expectedTarget) {
63
+ assert.isEqualDeep(passedTarget, expectedTarget, 'The target you passed did not match!');
64
+ }
65
+ if (expectedPayload) {
66
+ assert.isEqualDeep(passedPayload, expectedPayload, 'The payload you passed did not match!');
67
+ }
68
+ let cards = [];
69
+ try {
70
+ cards = vcAssert.assertSkillViewRendersCards(svc, [vc1Id, vc2Id]);
71
+ }
72
+ catch (err) {
73
+ assert.fail(`You didn't drop in the cards to your skill view! Make sure your vcIdsTransformer looks something like:
74
+
75
+ vcIdsTransformer: (payload) => {
76
+ return payload.vcIds
77
+ }
78
+
79
+ You'll also want to add the cards to a local prop like:
80
+
81
+ this.cards.push(...cards)
82
+
83
+ Then make sure you render the cards like:
84
+
85
+ public render() {
86
+ return {
87
+ layouts: splitCardsIntoLayouts(this.cards.map((c) => c.render()))
88
+ }
89
+ }
90
+
91
+ The original error is: ${(_a = err.stack) !== null && _a !== void 0 ? _a : err.message}
92
+ `);
93
+ }
94
+ if (shouldInvoke) {
95
+ const { methodName, expectedParams } = shouldInvoke;
96
+ assert.isTrue(
97
+ //@ts-ignore
98
+ cards.filter((c) => c.wasHit).length === cards.length, `You gotta call cardVc.${methodName}(...) on all the cards you get back!`);
99
+ if (expectedParams) {
100
+ //@ts-ignore
101
+ const passed = cards[0].passedParams;
102
+ assert.isEqualDeep(passed, expectedParams, `You didn't pass the expected params to ${methodName}!`);
103
+ }
104
+ }
105
+ });
106
+ },
107
+ };
108
+ function generateVcSource(options) {
109
+ const { vcId, count, stubMethod } = options;
110
+ return [
111
+ `class TestEventViewController${count} {
112
+ alert = function() {}
113
+ ${stubMethod !== null && stubMethod !== void 0 ? stubMethod : 'noop'} = function () {
114
+ this.wasHit = true
115
+ this.passedParams = Array.prototype.slice.call(arguments)
116
+ }
117
+ render = function() {
118
+ return {
119
+ id: '${vcId}',
120
+ controller: this,
121
+ }
122
+ }
123
+ }`,
124
+ `TestEventViewController${count}.id = '${vcId}'`,
125
+ ].join('\n\n');
126
+ }
127
+ export function generateId() {
128
+ return `${Math.round(new Date().getTime() * (Math.random() * 100))}`;
129
+ }
130
+ class ThrowingErrorCardVc extends AbstractViewController {
131
+ constructor(options) {
132
+ super(options);
133
+ //@ts-ignore
134
+ if (options.error) {
135
+ //@ts-ignore
136
+ throw options.error;
137
+ }
138
+ }
139
+ render() {
140
+ return {};
141
+ }
142
+ }
143
+ export default remoteVcAssert;
@@ -62,7 +62,11 @@ export default class CardRegistrar {
62
62
  }
63
63
  catch (err) {
64
64
  console.error(err);
65
- return [this.remoteVcFactory.Controller('heartwood.error-card', {})];
65
+ return [
66
+ this.remoteVcFactory.Controller('heartwood.error-card', {
67
+ error: err,
68
+ }),
69
+ ];
66
70
  }
67
71
  });
68
72
  }
@@ -1,2 +1,3 @@
1
1
  export { default as RemoteViewControllerFactory } from './viewControllers/RemoteViewControllerFactory';
2
2
  export { default as CardRegistrar } from './viewControllers/CardRegistrar';
3
+ export { default as remoteVcAssert } from './tests/remoteVcAssert';
@@ -3,9 +3,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.CardRegistrar = exports.RemoteViewControllerFactory = void 0;
6
+ exports.remoteVcAssert = exports.CardRegistrar = exports.RemoteViewControllerFactory = void 0;
7
7
  var RemoteViewControllerFactory_1 = require("./viewControllers/RemoteViewControllerFactory");
8
8
  Object.defineProperty(exports, "RemoteViewControllerFactory", { enumerable: true, get: function () { return __importDefault(RemoteViewControllerFactory_1).default; } });
9
9
  var CardRegistrar_1 = require("./viewControllers/CardRegistrar");
10
10
  Object.defineProperty(exports, "CardRegistrar", { enumerable: true, get: function () { return __importDefault(CardRegistrar_1).default; } });
11
+ var remoteVcAssert_1 = require("./tests/remoteVcAssert");
12
+ Object.defineProperty(exports, "remoteVcAssert", { enumerable: true, get: function () { return __importDefault(remoteVcAssert_1).default; } });
11
13
  //# sourceMappingURL=index-module.js.map
@@ -0,0 +1,21 @@
1
+ import { SkillViewController } from '@sprucelabs/heartwood-view-controllers';
2
+ import { SkillEventContract } from '@sprucelabs/mercury-types';
3
+ import { ArgsFromSvc, ViewFixture } from '@sprucelabs/spruce-test-fixtures';
4
+ declare type EventNames = keyof SkillEventContract['eventSignatures'];
5
+ export interface AssertRendersRemoteCardsOptions<Svc extends SkillViewController = SkillViewController> {
6
+ svc: Svc;
7
+ fqen: EventNames;
8
+ views: ViewFixture;
9
+ expectedTarget?: Record<string, any>;
10
+ expectedPayload?: Record<string, any>;
11
+ loadArgs?: ArgsFromSvc<Svc>;
12
+ shouldInvoke?: {
13
+ methodName: string;
14
+ expectedParams?: any[];
15
+ };
16
+ }
17
+ declare const remoteVcAssert: {
18
+ assertSkillViewRendersRemoteCards<Svc extends SkillViewController<Record<string, any>> = SkillViewController<Record<string, any>>>(options: AssertRendersRemoteCardsOptions<Svc>): Promise<void>;
19
+ };
20
+ export declare function generateId(): string;
21
+ export default remoteVcAssert;
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateId = void 0;
4
+ const heartwood_view_controllers_1 = require("@sprucelabs/heartwood-view-controllers");
5
+ const schema_1 = require("@sprucelabs/schema");
6
+ const spruce_test_fixtures_1 = require("@sprucelabs/spruce-test-fixtures");
7
+ const test_1 = require("@sprucelabs/test");
8
+ const remoteVcAssert = {
9
+ async assertSkillViewRendersRemoteCards(options) {
10
+ var _a;
11
+ const { views, svc: svc, expectedTarget, expectedPayload, fqen, shouldInvoke, loadArgs, } = (0, schema_1.assertOptions)(options, [
12
+ 'svc',
13
+ 'fqen',
14
+ 'views',
15
+ ]);
16
+ const factory = views.getFactory();
17
+ factory.setController('heartwood.error-card', ThrowingErrorCardVc);
18
+ const vc1Id = generateId();
19
+ const vc2Id = generateId();
20
+ let wasHit = false;
21
+ let passedTarget;
22
+ let passedPayload;
23
+ await spruce_test_fixtures_1.eventFaker.on(fqen,
24
+ //@ts-ignore
25
+ async ({ target, payload }) => {
26
+ passedTarget = target;
27
+ passedPayload = payload;
28
+ wasHit = true;
29
+ return {
30
+ vcIds: ['assert.' + vc1Id, 'assert.' + vc2Id],
31
+ };
32
+ });
33
+ await spruce_test_fixtures_1.eventFaker.on('heartwood.get-skill-views::v2021_02_11', () => {
34
+ const source = [
35
+ generateVcSource({
36
+ vcId: vc1Id,
37
+ count: 1,
38
+ stubMethod: shouldInvoke === null || shouldInvoke === void 0 ? void 0 : shouldInvoke.methodName,
39
+ }),
40
+ generateVcSource({
41
+ vcId: vc2Id,
42
+ count: 2,
43
+ stubMethod: shouldInvoke === null || shouldInvoke === void 0 ? void 0 : shouldInvoke.methodName,
44
+ }),
45
+ `heartwood({ TestEventViewController1, TestEventViewController2 })`,
46
+ ].join('\n');
47
+ return {
48
+ id: generateId(),
49
+ ids: [vc1Id, vc2Id],
50
+ source,
51
+ };
52
+ });
53
+ await views.load(svc, loadArgs);
54
+ test_1.assert.isTrue(wasHit, `You did not load any remote cards. Next step is to add the following to your skill view's load():\n\nconst registrar = new CardRegistrar(...)\nconst cards = await registrar.fetch(...)\n\nAlso, make sure you create the new event that you'll emit when trying to fetch remote cards, something like 'my-skill.register-cards::v2020_02_02'\n\n`);
55
+ if (expectedTarget) {
56
+ test_1.assert.isEqualDeep(passedTarget, expectedTarget, 'The target you passed did not match!');
57
+ }
58
+ if (expectedPayload) {
59
+ test_1.assert.isEqualDeep(passedPayload, expectedPayload, 'The payload you passed did not match!');
60
+ }
61
+ let cards = [];
62
+ try {
63
+ cards = heartwood_view_controllers_1.vcAssert.assertSkillViewRendersCards(svc, [vc1Id, vc2Id]);
64
+ }
65
+ catch (err) {
66
+ test_1.assert.fail(`You didn't drop in the cards to your skill view! Make sure your vcIdsTransformer looks something like:
67
+
68
+ vcIdsTransformer: (payload) => {
69
+ return payload.vcIds
70
+ }
71
+
72
+ You'll also want to add the cards to a local prop like:
73
+
74
+ this.cards.push(...cards)
75
+
76
+ Then make sure you render the cards like:
77
+
78
+ public render() {
79
+ return {
80
+ layouts: splitCardsIntoLayouts(this.cards.map((c) => c.render()))
81
+ }
82
+ }
83
+
84
+ The original error is: ${(_a = err.stack) !== null && _a !== void 0 ? _a : err.message}
85
+ `);
86
+ }
87
+ if (shouldInvoke) {
88
+ const { methodName, expectedParams } = shouldInvoke;
89
+ test_1.assert.isTrue(
90
+ //@ts-ignore
91
+ cards.filter((c) => c.wasHit).length === cards.length, `You gotta call cardVc.${methodName}(...) on all the cards you get back!`);
92
+ if (expectedParams) {
93
+ //@ts-ignore
94
+ const passed = cards[0].passedParams;
95
+ test_1.assert.isEqualDeep(passed, expectedParams, `You didn't pass the expected params to ${methodName}!`);
96
+ }
97
+ }
98
+ },
99
+ };
100
+ function generateVcSource(options) {
101
+ const { vcId, count, stubMethod } = options;
102
+ return [
103
+ `class TestEventViewController${count} {
104
+ alert = function() {}
105
+ ${stubMethod !== null && stubMethod !== void 0 ? stubMethod : 'noop'} = function () {
106
+ this.wasHit = true
107
+ this.passedParams = Array.prototype.slice.call(arguments)
108
+ }
109
+ render = function() {
110
+ return {
111
+ id: '${vcId}',
112
+ controller: this,
113
+ }
114
+ }
115
+ }`,
116
+ `TestEventViewController${count}.id = '${vcId}'`,
117
+ ].join('\n\n');
118
+ }
119
+ function generateId() {
120
+ return `${Math.round(new Date().getTime() * (Math.random() * 100))}`;
121
+ }
122
+ exports.generateId = generateId;
123
+ class ThrowingErrorCardVc extends heartwood_view_controllers_1.AbstractViewController {
124
+ constructor(options) {
125
+ super(options);
126
+ //@ts-ignore
127
+ if (options.error) {
128
+ //@ts-ignore
129
+ throw options.error;
130
+ }
131
+ }
132
+ render() {
133
+ return {};
134
+ }
135
+ }
136
+ exports.default = remoteVcAssert;
137
+ //# sourceMappingURL=remoteVcAssert.js.map
@@ -57,7 +57,11 @@ class CardRegistrar {
57
57
  }
58
58
  catch (err) {
59
59
  console.error(err);
60
- return [this.remoteVcFactory.Controller('heartwood.error-card', {})];
60
+ return [
61
+ this.remoteVcFactory.Controller('heartwood.error-card', {
62
+ error: err,
63
+ }),
64
+ ];
61
65
  }
62
66
  }
63
67
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sprucelabs/spruce-heartwood-utils",
3
3
  "description": "Heartwood Utilities",
4
- "version": "7.5.0",
4
+ "version": "7.7.0",
5
5
  "skill": {
6
6
  "namespace": "heartwood"
7
7
  },
@@ -29,7 +29,11 @@
29
29
  "build/ThemeManager.js",
30
30
  "build/ThemeManager.d.ts",
31
31
  "build/esm/ThemeManager.js",
32
- "build/esm/ThemeManager.d.ts"
32
+ "build/esm/ThemeManager.d.ts",
33
+ "build/tests/remoteVcAssert.js",
34
+ "build/tests/remoteVcAssert.d.ts",
35
+ "build/esm/tests/remoteVcAssert.js",
36
+ "build/esm/tests/remoteVcAssert.d.ts"
33
37
  ],
34
38
  "keywords": [],
35
39
  "scripts": {
@@ -38,7 +42,11 @@
38
42
  "dependencies": {
39
43
  "@sprucelabs/heartwood-view-controllers": "latest",
40
44
  "@sprucelabs/mercury-client": "latest",
41
- "@sprucelabs/spruce-event-utils": "latest"
45
+ "@sprucelabs/mercury-types": "latest",
46
+ "@sprucelabs/schema": "latest",
47
+ "@sprucelabs/spruce-event-utils": "latest",
48
+ "@sprucelabs/spruce-test-fixtures": "latest",
49
+ "@sprucelabs/test": "latest"
42
50
  },
43
51
  "engines": {
44
52
  "node": "16.x",