summaryrefslogtreecommitdiffstats
path: root/browser/components/pagedata/tests/unit/test_pagedata_basic.js
blob: 5d31645a4cd839e2f26f2f0b147419ae2b82cb43 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Simply tests that the notification is dispatched when new page data is
 * discovered.
 */

ChromeUtils.defineESModuleGetters(this, {
  PageDataService: "resource:///modules/pagedata/PageDataService.sys.mjs",
});

add_task(async function test_pageDataDiscovered_notifies() {
  let url = "https://www.mozilla.org/";

  Assert.equal(
    PageDataService.getCached(url),
    null,
    "Should be no cached data."
  );

  let promise = PageDataService.once("page-data");

  PageDataService.pageDataDiscovered({
    url,
    date: 32453456,
    data: {
      [PageDataSchema.DATA_TYPE.PRODUCT]: {
        name: "Bolts",
        price: { value: 276 },
      },
    },
  });

  let pageData = await promise;
  Assert.equal(
    pageData.url,
    url,
    "Should have notified data for the expected url"
  );

  Assert.deepEqual(
    pageData,
    {
      url,
      date: 32453456,
      data: {
        [PageDataSchema.DATA_TYPE.PRODUCT]: {
          name: "Bolts",
          price: { value: 276 },
        },
      },
    },
    "Should have returned the correct product data"
  );

  Assert.equal(
    PageDataService.getCached(url),
    null,
    "Should not have cached the data as there was no actor locking."
  );

  let actor = {};
  PageDataService.lockEntry(actor, url);

  PageDataService.pageDataDiscovered({
    url,
    date: 32453456,
    data: {
      [PageDataSchema.DATA_TYPE.PRODUCT]: {
        name: "Bolts",
        price: { value: 276 },
      },
    },
  });

  // Should now be in the cache.
  Assert.deepEqual(
    PageDataService.getCached(url),
    {
      url,
      date: 32453456,
      data: {
        [PageDataSchema.DATA_TYPE.PRODUCT]: {
          name: "Bolts",
          price: { value: 276 },
        },
      },
    },
    "Should have cached the data"
  );

  PageDataService.unlockEntry(actor, url);

  Assert.equal(
    PageDataService.getCached(url),
    null,
    "Should have dropped the data from the cache."
  );
});