summaryrefslogtreecommitdiffstats
path: root/devtools/client/application/test/xpcshell/test_manifest_reducer.js
blob: 2e91b57442ad0b12abff0307e4cb1025a1e88764 (plain)
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
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const {
  FETCH_MANIFEST_FAILURE,
  FETCH_MANIFEST_START,
  FETCH_MANIFEST_SUCCESS,
  RESET_MANIFEST,
  MANIFEST_MEMBER_VALUE_TYPES,
} = require("resource://devtools/client/application/src/constants.js");

const { ICON, COLOR, STRING, URL } = MANIFEST_MEMBER_VALUE_TYPES;

const {
  manifestReducer,
  ManifestState,
} = require("resource://devtools/client/application/src/reducers/manifest-state.js");

const MANIFEST_PROCESSING = [
  // empty manifest
  {
    source: {},
    processed: {},
  },
  // manifest with just one member
  {
    source: { name: "Foo" },
    processed: {
      identity: [{ key: "name", value: "Foo", type: STRING }],
    },
  },
  // manifest with two members from the same category
  {
    source: {
      short_name: "Short Foo",
      name: "Long Foo",
    },
    processed: {
      identity: [
        { key: "short_name", value: "Short Foo", type: STRING },
        { key: "name", value: "Long Foo", type: STRING },
      ],
    },
  },
  // manifest with members from two different categories
  {
    source: {
      name: "Foo",
      background_color: "#FF0000",
      start_url: "https://example.com/?q=foo",
      scope: "https://example.com",
    },
    processed: {
      identity: [{ key: "name", value: "Foo", type: STRING }],
      presentation: [
        { key: "background_color", value: "#FF0000", type: COLOR },
        { key: "start_url", value: "https://example.com/?q=foo", type: URL },
        { key: "scope", value: "https://example.com", type: URL },
      ],
    },
  },
  // manifest with icons
  {
    source: {
      icons: [
        {
          src: "something.png",
          type: "image/png",
          sizes: ["16x16", "32x32"],
          purpose: ["any"],
        },
        {
          src: "another.svg",
          type: "image/svg",
          sizes: ["any"],
          purpose: ["any maskable"],
        },
        {
          src: "something.png",
          type: undefined,
          sizes: undefined,
          purpose: ["any"],
        },
      ],
    },
    processed: {
      icons: [
        {
          key: { sizes: "16x16 32x32", contentType: "image/png" },
          value: { src: "something.png", purpose: "any" },
          type: ICON,
        },
        {
          key: { sizes: "any", contentType: "image/svg" },
          value: { src: "another.svg", purpose: "any maskable" },
          type: ICON,
        },
        {
          key: { sizes: undefined, contentType: undefined },
          value: { src: "something.png", purpose: "any" },
          type: ICON,
        },
      ],
    },
  },
  // manifest with issues
  {
    source: {
      moz_validation: [
        { warn: "A warning" },
        { error: "An error", type: "json" },
      ],
    },
    processed: {
      validation: [
        { level: "warning", message: "A warning", type: null },
        { level: "error", message: "An error", type: "json" },
      ],
    },
  },
  // manifest with URL
  {
    source: {
      moz_manifest_url: "https://example.com/manifest.json",
    },
    processed: {
      url: "https://example.com/manifest.json",
    },
  },
];

add_task(async function () {
  info("Test manifest reducer: FETCH_MANIFEST_START action");

  const state = ManifestState();
  const action = { type: FETCH_MANIFEST_START };
  const newState = manifestReducer(state, action);

  equal(newState.isLoading, true, "Loading flag is true");
});

add_task(async function () {
  info("Test manifest reducer: FETCH_MANIFEST_FAILURE action");

  const state = Object.assign(ManifestState(), { isLoading: true });
  const action = { type: FETCH_MANIFEST_FAILURE, error: "some error" };
  const newState = manifestReducer(state, action);

  equal(newState.errorMessage, "some error", "Error message is as expected");
  equal(newState.isLoading, false, "Loading flag is false");
  equal(newState.manifest, null, "Manifest is null");
});

add_task(async function () {
  info("Test manifest reducer: FETCH_MANIFEST_SUCCESS action");

  // test manifest processing
  MANIFEST_PROCESSING.forEach(({ source, processed }) => {
    test_manifest_processing(source, processed);
  });
});

add_task(async function () {
  info("Test manifest reducer: RESET_MANIFEST action");

  const state = Object.assign(ManifestState(), {
    isLoading: true,
    manifest: { identity: [{ key: "name", value: "Foo" }] },
    errorMessage: "some error",
  });
  const action = { type: RESET_MANIFEST };
  const newState = manifestReducer(state, action);

  deepEqual(newState, ManifestState(), "Manifest has been reset to defaults");
});

function test_manifest_processing(source, processed) {
  const state = ManifestState();
  state.isLoading = true;

  const action = { type: FETCH_MANIFEST_SUCCESS, manifest: source };
  const newState = manifestReducer(state, action);

  // merge the expected processed manifst with some default values
  const expected = Object.assign(
    {
      icons: [],
      identity: [],
      presentation: [],
      url: undefined,
      validation: [],
    },
    processed
  );

  deepEqual(newState.manifest, expected, "Processed manifest as expected");
  equal(newState.errorMessage, "", "Error message is empty");
  equal(newState.isLoading, false, "Loading flag is false");
}