discourse/test/javascripts/lib/oneboxer-test.js.es6
Mark VanLandingham 176aa0ac7d
DEV: Import pretender instead of global server var (#8996)
* DEV: Remove server global test variable

* Delete yarn-error.log

* prettier and some eslint fixes

* add global server variable back for plugins

* rename imported server to pretender

* prettier

* support plugin server. usage

* Export pretender as named

* Prettier

* change default pretender export

* fix bad import

* Use pretender() and original default export

* export new Pretender as default

* fix accidental change

* WIP testing

* add pretend handlers in correct location

* move more stuff into the correct pretender

* Consolidated more pretenders

* comment out another bad test

* fix user acceptance tests

* commented out bad test

* fixed another composer server stub

* fix more tests

* fixed tag test pretender

* Fix admin email test

* removed another draft handler

* add back test

* fix and uncomment another test

* remove test that is not useful

* remove commented out lines

* reapply handlers between every test

* no need to re-stub requests now :)

* cleanup from review

* more cleanup
2020-03-02 14:24:31 -05:00

69 lines
1.8 KiB
JavaScript

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");
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 = `
<aside class="onebox whitelistedgeneric">
<header class="source">
<a href="http://test.com/somepage" target="_blank">test.com</a>
</header>
<article class="onebox-body">
<div class="aspect-image" style="--aspect-ratio:690/362;"><img src="https://test.com/image.png" class="thumbnail"></div>
<h3><a href="http://test.com/somepage" target="_blank">Test Page</a></h3>
<p>Yet another collaboration tool</p>
</article>
<div class="onebox-metadata"></div>
<div style="clear: both"></div>
</aside>
`;
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),
html.trim(),
"it returns the html from the cache"
);
});