summaryrefslogtreecommitdiffstats
path: root/toolkit/components/shopping/test/browser/head.js
blob: f2b303ce12af631c9f2e7781987c1f1b3ea1c514 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const PRODUCT_TEST_URL = "https://example.com/Some-Product/dp/ABCDEFG123";
const OTHER_PRODUCT_TEST_URL =
  "https://example.com/Another-Product/dp/HIJKLMN456";
const BAD_PRODUCT_TEST_URL = "https://example.com/Bad-Product/dp/0000000000";
const NEEDS_ANALYSIS_TEST_URL = "https://example.com/Bad-Product/dp/OPQRSTU789";

async function promiseSidebarUpdated(sidebar, expectedProduct) {
  let browser = sidebar.querySelector("browser");
  if (
    !browser.currentURI?.equals(Services.io.newURI("about:shoppingsidebar"))
  ) {
    await BrowserTestUtils.browserLoaded(
      browser,
      false,
      "about:shoppingsidebar"
    );
  }
  return SpecialPowers.spawn(browser, [expectedProduct], prod => {
    function isProductCurrent() {
      let actor = content.windowGlobalChild.getExistingActor("ShoppingSidebar");
      return actor?.getProductURI()?.spec == prod;
    }
    if (
      isProductCurrent() &&
      !!content.document.querySelector("shopping-container").wrappedJSObject
        .data
    ) {
      info("Product already loaded.");
      return true;
    }
    info(
      "Waiting for product to be updated. Document: " +
        content.document.location.href
    );
    return ContentTaskUtils.waitForEvent(
      content.document,
      "Update",
      true,
      e => {
        info("Sidebar updated for product: " + JSON.stringify(e.detail));
        return !!e.detail.data && isProductCurrent();
      },
      true
    ).then(() => true);
  });
}

async function promiseSidebarAdsUpdated(sidebar, expectedProduct) {
  await promiseSidebarUpdated(sidebar, expectedProduct);
  let browser = sidebar.querySelector("browser");
  return SpecialPowers.spawn(browser, [], () => {
    let container =
      content.document.querySelector("shopping-container").wrappedJSObject;
    if (container.recommendationData) {
      return true;
    }
    return ContentTaskUtils.waitForEvent(
      content.document,
      "UpdateRecommendations",
      true,
      null,
      true
    ).then(() => true);
  });
}

async function verifyProductInfo(sidebar, expectedProductInfo) {
  await SpecialPowers.spawn(
    sidebar.querySelector("browser"),
    [expectedProductInfo],
    async prodInfo => {
      let doc = content.document;
      let container = doc.querySelector("shopping-container");
      let root = container.shadowRoot;
      let reviewReliability = root.querySelector("review-reliability");
      // The async fetch could take some time.
      while (!reviewReliability) {
        info("Waiting for update.");
        await container.updateComplete;
      }
      let adjustedRating = root.querySelector("adjusted-rating");
      Assert.equal(
        reviewReliability.getAttribute("letter"),
        prodInfo.letterGrade,
        `Should have correct letter grade for product ${prodInfo.id}.`
      );
      Assert.equal(
        adjustedRating.getAttribute("rating"),
        prodInfo.adjustedRating,
        `Should have correct adjusted rating for product ${prodInfo.id}.`
      );
      Assert.equal(
        content.windowGlobalChild
          .getExistingActor("ShoppingSidebar")
          ?.getProductURI()?.spec,
        prodInfo.productURL,
        `Should have correct url in the child.`
      );
    }
  );
}