Commit 97d16952 authored by Nan Jiang's avatar Nan Jiang
Browse files

Bug 1822604 - Fix an edge case for spoc topsites injection r=thecount

parent 633f202e
Loading
Loading
Loading
Loading
+3 −10
Original line number Diff line number Diff line
@@ -920,16 +920,9 @@ class TopSitesFeed {
        return;
      }
      let index = link.sponsored_position - 1;
      // For DiscoveryStream spocs, we use a different position property
      if (link.type === "SPOC") {
        index = link.pos;
      }
      if (index > withPinned.length) {
      if (index >= withPinned.length) {
        withPinned[index] = link;
      } else if (
        link.type === "SPOC" &&
        withPinned[index].show_sponsored_label
      ) {
      } else if (withPinned[index].sponsored_position) {
        // We currently want DiscoveryStream spocs to replace existing spocs.
        withPinned[index] = link;
      } else {
@@ -1310,7 +1303,7 @@ class TopSitesFeed {
      const link = this._linksWithDefaults[i];
      if (
        link &&
        (link.sponsored_position || link.type === "SPOC") &&
        link.sponsored_position &&
        this._linksWithDefaults[i]?.url !== site.url
      ) {
        adjustedIndex--;
+29 −26
Original line number Diff line number Diff line
@@ -616,8 +616,7 @@ describe("Top Sites Feed", () => {
      assert.calledWith(feed._fetchScreenshot, sinon.match.object, "custom");
    });
    describe("discoverystream", () => {
      beforeEach(() => {
        feed.store.state.DiscoveryStream = {
      let makeStreamData = index => ({
        layout: [
          {
            components: [
@@ -626,7 +625,7 @@ describe("Top Sites Feed", () => {
                  name: "sponsored-topsites",
                },
                spocs: {
                    positions: [{ index: 1 }],
                  positions: [{ index }],
                },
              },
            ],
@@ -639,14 +638,18 @@ describe("Top Sites Feed", () => {
            },
          },
        },
        };
      });
      it("should add a sponsored topsite from discoverystream", async () => {
      it("should add a sponsored topsite from discoverystream to all the valid indices", async () => {
        for (let i = 0; i < FAKE_LINKS.length; i++) {
          feed.store.state.DiscoveryStream = makeStreamData(i);
          const result = await feed.getLinksWithDefaults();
        assert.equal(result[1].type, "SPOC");
        assert.equal(result[1].title, "test spoc");
        assert.equal(result[1].sponsored_position, 2);
        assert.equal(result[1].url, "https://test-spoc.com");
          const link = result[i];

          assert.equal(link.type, "SPOC");
          assert.equal(link.title, "test spoc");
          assert.equal(link.sponsored_position, i + 1);
          assert.equal(link.url, "https://test-spoc.com");
        }
      });
    });
  });