SafeAnchor.test.jsx (2751B)
1 import React from "react"; 2 import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor"; 3 import { shallow } from "enzyme"; 4 5 describe("Discovery Stream <SafeAnchor>", () => { 6 let warnStub; 7 let sandbox; 8 beforeEach(() => { 9 warnStub = sinon.stub(console, "warn"); 10 sandbox = sinon.createSandbox(); 11 }); 12 afterEach(() => { 13 warnStub.restore(); 14 sandbox.restore(); 15 }); 16 it("should render with anchor", () => { 17 const wrapper = shallow(<SafeAnchor />); 18 assert.lengthOf(wrapper.find("a"), 1); 19 }); 20 it("should render with anchor target for http", () => { 21 const wrapper = shallow(<SafeAnchor url="http://example.com" />); 22 assert.equal(wrapper.find("a").prop("href"), "http://example.com"); 23 }); 24 it("should render with anchor target for https", () => { 25 const wrapper = shallow(<SafeAnchor url="https://example.com" />); 26 assert.equal(wrapper.find("a").prop("href"), "https://example.com"); 27 }); 28 it("should not allow javascript: URIs", () => { 29 const wrapper = shallow(<SafeAnchor url="javascript:foo()" />); // eslint-disable-line no-script-url 30 assert.equal(wrapper.find("a").prop("href"), ""); 31 assert.calledOnce(warnStub); 32 }); 33 it("should not warn if the URL is falsey ", () => { 34 const wrapper = shallow(<SafeAnchor url="" />); 35 assert.equal(wrapper.find("a").prop("href"), ""); 36 assert.notCalled(warnStub); 37 }); 38 it("should dispatch an event on click", () => { 39 const dispatchStub = sandbox.stub(); 40 const fakeEvent = { preventDefault: sandbox.stub(), currentTarget: {} }; 41 const wrapper = shallow(<SafeAnchor dispatch={dispatchStub} />); 42 43 wrapper.find("a").simulate("click", fakeEvent); 44 45 assert.calledOnce(dispatchStub); 46 assert.calledOnce(fakeEvent.preventDefault); 47 }); 48 it("should call onLinkClick if provided", () => { 49 const onLinkClickStub = sandbox.stub(); 50 const wrapper = shallow(<SafeAnchor onLinkClick={onLinkClickStub} />); 51 52 wrapper.find("a").simulate("click"); 53 54 assert.calledOnce(onLinkClickStub); 55 }); 56 it("should render data-is-sponsored-link='false' when isSponsored is false", () => { 57 const wrapper = shallow(<SafeAnchor isSponsored={false} />); 58 assert.equal(wrapper.find("a").prop("data-is-sponsored-link"), false); 59 }); 60 it("should render data-is-sponsored-link='false' when isSponsored is not provided", () => { 61 const wrapper = shallow(<SafeAnchor />); 62 assert.equal(wrapper.find("a").prop("data-is-sponsored-link"), false); 63 }); 64 it("should render data-is-sponsored-link='true' when isSponsored is true", () => { 65 const wrapper = shallow(<SafeAnchor isSponsored={true} />); 66 assert.equal(wrapper.find("a").prop("data-is-sponsored-link"), true); 67 }); 68 });