diff --git a/src/readthedocs-config.js b/src/readthedocs-config.js index 01fdb446..efb953e1 100644 --- a/src/readthedocs-config.js +++ b/src/readthedocs-config.js @@ -69,17 +69,19 @@ export function getReadTheDocsUserConfig(sendUrlParam) { // this data is ready to be consumed under `event.detail.data()`. const userApiUrl = _getApiUrl(sendUrlParam, metadataAddonsAPIVersion); - fetch(userApiUrl, { - method: "GET", - }).then((response) => { - if (!response.ok) { - return reject( - "Error hitting addons API endpoint for user api-version", - ); - } - // Return the data in the API version requested. - return resolve(response.json()); - }); + return resolve( + fetch(userApiUrl, { + method: "GET", + }).then((response) => { + if (!response.ok) { + return reject( + "Error hitting addons API endpoint for user api-version", + ); + } + // Return the data in the API version requested. + return response.json(); + }), + ); } // If the API versions match, we return `undefined`. @@ -124,8 +126,8 @@ export function getReadTheDocsConfig(sendUrlParam) { // event was fired can still get access to the data globalThis.ReadTheDocsEventData = new ReadTheDocsEventData(dataEvent); - // Trigger the addons data ready CustomEvent to with the data the user is expecting. - return dispatchEvent( + // Trigger the addons data ready CustomEvent with the data the user is expecting. + dispatchEvent( EVENT_READTHEDOCS_ADDONS_USER_DATA_READY, document, new ReadTheDocsEventData(dataEvent), diff --git a/tests/__snapshots__/search.test.snap.js b/tests/__snapshots__/search.test.snap.js index 0c8514de..c268cd51 100644 --- a/tests/__snapshots__/search.test.snap.js +++ b/tests/__snapshots__/search.test.snap.js @@ -1,12 +1,11 @@ /* @web/test-runner snapshot v1 */ export const snapshots = {}; - snapshots["Search tests initial trigger by event and DOM check"] = `
-
+ + + + + + + + diff --git a/tests/readthedocs-config.test.html b/tests/readthedocs-config.test.html index d0131e79..ecaa6dd7 100644 --- a/tests/readthedocs-config.test.html +++ b/tests/readthedocs-config.test.html @@ -9,6 +9,7 @@ import { expect, elementUpdated } from "@open-wc/testing"; import { runTests } from "@web/test-runner-mocha"; import * as readthedocsConfig from "../src/readthedocs-config"; + import * as events from "../src/events"; import * as utils from "../src/utils"; let server; @@ -20,6 +21,8 @@ beforeEach(() => { // Create a sinon fake server to mock requests server = sinon.fakeServer.create(); + server.respondImmediately = true; + if (metadataAddonsAPIVersion) { metadataAddonsAPIVersion.remove(); } @@ -54,17 +57,10 @@ it("fetch config from API endpoint", async () => { const matchMockedUrl = new RegExp(`^/_/addons/`, "g"); - server.respondWith("GET", matchMockedUrl, [ - 200, - {}, - "fake response body", - ]); + server.respondWith("GET", matchMockedUrl, [200, {}, "{}"]); const config = readthedocsConfig.getReadTheDocsConfig(false); - // Respond to all the request waiting for a response - server.respond(); - expect(server.requests).to.have.length(1); expect(server.requests[0].status).to.be.equal(200); expect(server.requests[0].method).to.be.equal("GET"); @@ -78,18 +74,22 @@ metadataAddonsAPIVersion.content = "2"; document.head.appendChild(metadataAddonsAPIVersion); - // Response 204 on requests made to Read the Docs analytics' API + // Response 200 on requests made to Read the Docs analytics' API const matchMockedUrl = new RegExp(`^/_/addons/`, "g"); server.respondWith("GET", matchMockedUrl, [ 200, {}, - "fake response body", + '{"addons": {}, "builds": {"current": {"id": 12345}}, "projects": {}, "versions": {}}', ]); - const config = readthedocsConfig.getReadTheDocsUserConfig(true); - - // Respond to all the request waiting for a response - server.respond(); + const config = + await readthedocsConfig.getReadTheDocsUserConfig(true); + expect(config).to.deep.equal({ + addons: {}, + builds: { current: { id: 12345 } }, + projects: {}, + versions: {}, + }); expect(server.requests).to.have.length(1); expect(server.requests[0].status).to.be.equal(200); @@ -98,6 +98,40 @@ /^\/_\/addons\/\?client-version=.+&api-version=2&url=.+$/; expect(server.requests[0].url).to.match(matchApiUrl); }); + + it("check readthedocs-addons-data-ready event", async () => { + // Response 200 on requests made to Read the Docs analytics' API + const matchMockedUrl = new RegExp(`^/_/addons/`, "g"); + server.respondWith("GET", matchMockedUrl, [ + 200, + {}, + '{"test": "readthedocs-addons-data-ready", "builds": {"current": {"id": 12345}}}', + ]); + + let eventData; + const fakeFunction = sinon.fake(); + document.addEventListener( + events.EVENT_READTHEDOCS_ADDONS_USER_DATA_READY, + (event) => { + // Store the event data to check later + eventData = event.detail.data(true); + + // Call the `fakeFunction` inside the event listener to check later it was called once. + fakeFunction(); + }, + ); + + const config = await readthedocsConfig.getReadTheDocsConfig(true); + expect(fakeFunction.calledOnce).to.be.true; + expect(eventData).to.deep.equal({ + test: "readthedocs-addons-data-ready", + builds: { + current: { + id: 12345, + }, + }, + }); + }); }); }); diff --git a/tests/search.test.html b/tests/search.test.html index 01b0a454..98bbbc0c 100644 --- a/tests/search.test.html +++ b/tests/search.test.html @@ -60,7 +60,9 @@ document.dispatchEvent(searchEvent); await elementUpdated(element); - await expect(element).shadowDom.to.equalSnapshot(); + await expect(element).shadowDom.to.equalSnapshot({ + ignoreAttributes: [{ tags: ["form"], attributes: ["class"] }], + }); }); it("initial trigger by event and DOM check with filters", async () => { diff --git a/tests/utils.test.html b/tests/utils.test.html index e140f889..079bc430 100644 --- a/tests/utils.test.html +++ b/tests/utils.test.html @@ -4,8 +4,10 @@