1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
import {
actionCreators as ac,
actionTypes as at,
actionUtils as au,
BACKGROUND_PROCESS,
CONTENT_MESSAGE_TYPE,
globalImportContext,
MAIN_MESSAGE_TYPE,
PRELOAD_MESSAGE_TYPE,
UI_CODE,
} from "common/Actions.mjs";
describe("Actions", () => {
it("should set globalImportContext to UI_CODE", () => {
assert.equal(globalImportContext, UI_CODE);
});
});
describe("ActionTypes", () => {
it("should be in alpha order", () => {
assert.equal(Object.keys(at).join(", "), Object.keys(at).sort().join(", "));
});
});
describe("ActionCreators", () => {
describe("_RouteMessage", () => {
it("should throw if options are not passed as the second param", () => {
assert.throws(() => {
au._RouteMessage({ type: "FOO" });
});
});
it("should set all defined options on the .meta property of the new action", () => {
assert.deepEqual(
au._RouteMessage(
{ type: "FOO", meta: { hello: "world" } },
{ from: "foo", to: "bar" }
),
{ type: "FOO", meta: { hello: "world", from: "foo", to: "bar" } }
);
});
it("should remove any undefined options related to message routing", () => {
const action = au._RouteMessage(
{ type: "FOO", meta: { fromTarget: "bar" } },
{ from: "foo", to: "bar" }
);
assert.isUndefined(action.meta.fromTarget);
});
});
describe("AlsoToMain", () => {
it("should create the right action", () => {
const action = { type: "FOO", data: "BAR" };
const newAction = ac.AlsoToMain(action);
assert.deepEqual(newAction, {
type: "FOO",
data: "BAR",
meta: { from: CONTENT_MESSAGE_TYPE, to: MAIN_MESSAGE_TYPE },
});
});
it("should add the fromTarget if it was supplied", () => {
const action = { type: "FOO", data: "BAR" };
const newAction = ac.AlsoToMain(action, "port123");
assert.equal(newAction.meta.fromTarget, "port123");
});
describe("isSendToMain", () => {
it("should return true if action is AlsoToMain", () => {
const newAction = ac.AlsoToMain({ type: "FOO" });
assert.isTrue(au.isSendToMain(newAction));
});
it("should return false if action is not AlsoToMain", () => {
assert.isFalse(au.isSendToMain({ type: "FOO" }));
});
});
});
describe("AlsoToOneContent", () => {
it("should create the right action", () => {
const action = { type: "FOO", data: "BAR" };
const targetId = "abc123";
const newAction = ac.AlsoToOneContent(action, targetId);
assert.deepEqual(newAction, {
type: "FOO",
data: "BAR",
meta: {
from: MAIN_MESSAGE_TYPE,
to: CONTENT_MESSAGE_TYPE,
toTarget: targetId,
},
});
});
it("should throw if no targetId is provided", () => {
assert.throws(() => {
ac.AlsoToOneContent({ type: "FOO" });
});
});
describe("isSendToOneContent", () => {
it("should return true if action is AlsoToOneContent", () => {
const newAction = ac.AlsoToOneContent({ type: "FOO" }, "foo123");
assert.isTrue(au.isSendToOneContent(newAction));
});
it("should return false if action is not AlsoToMain", () => {
assert.isFalse(au.isSendToOneContent({ type: "FOO" }));
assert.isFalse(
au.isSendToOneContent(ac.BroadcastToContent({ type: "FOO" }))
);
});
});
describe("isFromMain", () => {
it("should return true if action is AlsoToOneContent", () => {
const newAction = ac.AlsoToOneContent({ type: "FOO" }, "foo123");
assert.isTrue(au.isFromMain(newAction));
});
it("should return true if action is BroadcastToContent", () => {
const newAction = ac.BroadcastToContent({ type: "FOO" });
assert.isTrue(au.isFromMain(newAction));
});
it("should return false if action is AlsoToMain", () => {
const newAction = ac.AlsoToMain({ type: "FOO" });
assert.isFalse(au.isFromMain(newAction));
});
});
});
describe("BroadcastToContent", () => {
it("should create the right action", () => {
const action = { type: "FOO", data: "BAR" };
const newAction = ac.BroadcastToContent(action);
assert.deepEqual(newAction, {
type: "FOO",
data: "BAR",
meta: { from: MAIN_MESSAGE_TYPE, to: CONTENT_MESSAGE_TYPE },
});
});
describe("isBroadcastToContent", () => {
it("should return true if action is BroadcastToContent", () => {
assert.isTrue(
au.isBroadcastToContent(ac.BroadcastToContent({ type: "FOO" }))
);
});
it("should return false if action is not BroadcastToContent", () => {
assert.isFalse(au.isBroadcastToContent({ type: "FOO" }));
assert.isFalse(
au.isBroadcastToContent(
ac.AlsoToOneContent({ type: "FOO" }, "foo123")
)
);
});
});
});
describe("AlsoToPreloaded", () => {
it("should create the right action", () => {
const action = { type: "FOO", data: "BAR" };
const newAction = ac.AlsoToPreloaded(action);
assert.deepEqual(newAction, {
type: "FOO",
data: "BAR",
meta: { from: MAIN_MESSAGE_TYPE, to: PRELOAD_MESSAGE_TYPE },
});
});
});
describe("isSendToPreloaded", () => {
it("should return true if action is AlsoToPreloaded", () => {
assert.isTrue(au.isSendToPreloaded(ac.AlsoToPreloaded({ type: "FOO" })));
});
it("should return false if action is not AlsoToPreloaded", () => {
assert.isFalse(au.isSendToPreloaded({ type: "FOO" }));
assert.isFalse(
au.isSendToPreloaded(ac.BroadcastToContent({ type: "FOO" }))
);
});
});
describe("UserEvent", () => {
it("should include the given data", () => {
const data = { action: "foo" };
assert.equal(ac.UserEvent(data).data, data);
});
it("should wrap with AlsoToMain", () => {
const action = ac.UserEvent({ action: "foo" });
assert.isTrue(au.isSendToMain(action), "isSendToMain");
});
});
describe("ImpressionStats", () => {
it("should include the right data", () => {
const data = { action: "foo" };
assert.equal(ac.ImpressionStats(data).data, data);
});
it("should wrap with AlsoToMain if in UI code", () => {
assert.isTrue(
au.isSendToMain(ac.ImpressionStats({ action: "foo" })),
"isSendToMain"
);
});
it("should not wrap with AlsoToMain if not in UI code", () => {
const action = ac.ImpressionStats({ action: "foo" }, BACKGROUND_PROCESS);
assert.isFalse(au.isSendToMain(action), "isSendToMain");
});
});
describe("WebExtEvent", () => {
it("should set the provided type", () => {
const action = ac.WebExtEvent(at.WEBEXT_CLICK, {
source: "MyExtension",
url: "foo.com",
});
assert.equal(action.type, at.WEBEXT_CLICK);
});
it("should set the provided data", () => {
const data = { source: "MyExtension", url: "foo.com" };
const action = ac.WebExtEvent(at.WEBEXT_CLICK, data);
assert.equal(action.data, data);
});
it("should throw if the 'source' property is missing", () => {
assert.throws(() => {
ac.WebExtEvent(at.WEBEXT_CLICK, {});
});
});
});
});
describe("ActionUtils", () => {
describe("getPortIdOfSender", () => {
it("should return the PortID from a AlsoToMain action", () => {
const portID = "foo123";
const result = au.getPortIdOfSender(
ac.AlsoToMain({ type: "FOO" }, portID)
);
assert.equal(result, portID);
});
});
});
|