From c54be3ff25e0b25043487204ff10edab67b046b0 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Thu, 28 Nov 2019 08:20:32 +1000 Subject: [PATCH] Add tests for oneboxer.js.es6 (#8418) --- test/javascripts/lib/oneboxer-test.js.es6 | 78 +++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 test/javascripts/lib/oneboxer-test.js.es6 diff --git a/test/javascripts/lib/oneboxer-test.js.es6 b/test/javascripts/lib/oneboxer-test.js.es6 new file mode 100644 index 00000000000..77ca103827d --- /dev/null +++ b/test/javascripts/lib/oneboxer-test.js.es6 @@ -0,0 +1,78 @@ +import { load } from "pretty-text/oneboxer"; +import { ajax } from "discourse/lib/ajax"; +import { failedCache, localCache } from "pretty-text/oneboxer-cache"; +import { stringToHTML } from "helpers/html-helper"; + +function loadOnebox(element) { + return load({ + elem: element, + refresh: false, + ajax, + synchronous: true, + categoryId: 1, + topicId: 1 + }); +} + +QUnit.module("lib:oneboxer"); + +QUnit.test("load - failed onebox", async assert => { + let element = document.createElement("A"); + element.setAttribute("href", "http://somebadurl.com"); + + // prettier-ignore + server.get("/onebox", () => { //eslint-disable-line + return [404, {}, {}]; + }); + + await loadOnebox(element); + + assert.equal( + failedCache["http://somebadurl.com"], + true, + "stores the url as failed in a cache" + ); + assert.equal( + loadOnebox(element), + undefined, + "it returns early for a failed cache" + ); +}); + +QUnit.test("load - successful onebox", async assert => { + const html = ` + + `; + + // prettier-ignore + server.get("/onebox", () => { //eslint-disable-line + return [200, {}, html]; + }); + + let element = document.createElement("A"); + element.setAttribute("href", "http://somegoodurl.com"); + + await loadOnebox(element); + + assert.equal( + localCache["http://somegoodurl.com"].prop("outerHTML"), + stringToHTML(html).outerHTML, + "stores the html of the onebox in a local cache" + ); + assert.equal( + loadOnebox(element), + stringToHTML(html).outerHTML, + "it returns the html from the cache" + ); +});