summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/content-src/components/DiscoveryStreamComponents/SafeAnchor.test.jsx
blob: 5d643869b81391f3b1f3c5330b9c1816a4235f72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React from "react";
import { SafeAnchor } from "content-src/components/DiscoveryStreamComponents/SafeAnchor/SafeAnchor";
import { shallow } from "enzyme";

describe("Discovery Stream <SafeAnchor>", () => {
  let warnStub;
  let sandbox;
  beforeEach(() => {
    warnStub = sinon.stub(console, "warn");
    sandbox = sinon.createSandbox();
  });
  afterEach(() => {
    warnStub.restore();
    sandbox.restore();
  });
  it("should render with anchor", () => {
    const wrapper = shallow(<SafeAnchor />);
    assert.lengthOf(wrapper.find("a"), 1);
  });
  it("should render with anchor target for http", () => {
    const wrapper = shallow(<SafeAnchor url="http://example.com" />);
    assert.equal(wrapper.find("a").prop("href"), "http://example.com");
  });
  it("should render with anchor target for https", () => {
    const wrapper = shallow(<SafeAnchor url="https://example.com" />);
    assert.equal(wrapper.find("a").prop("href"), "https://example.com");
  });
  it("should not allow javascript: URIs", () => {
    const wrapper = shallow(<SafeAnchor url="javascript:foo()" />); // eslint-disable-line no-script-url
    assert.equal(wrapper.find("a").prop("href"), "");
    assert.calledOnce(warnStub);
  });
  it("should not warn if the URL is falsey ", () => {
    const wrapper = shallow(<SafeAnchor url="" />);
    assert.equal(wrapper.find("a").prop("href"), "");
    assert.notCalled(warnStub);
  });
  it("should dispatch an event on click", () => {
    const dispatchStub = sandbox.stub();
    const fakeEvent = { preventDefault: sandbox.stub(), currentTarget: {} };
    const wrapper = shallow(<SafeAnchor dispatch={dispatchStub} />);

    wrapper.find("a").simulate("click", fakeEvent);

    assert.calledOnce(dispatchStub);
    assert.calledOnce(fakeEvent.preventDefault);
  });
  it("should call onLinkClick if provided", () => {
    const onLinkClickStub = sandbox.stub();
    const wrapper = shallow(<SafeAnchor onLinkClick={onLinkClickStub} />);

    wrapper.find("a").simulate("click");

    assert.calledOnce(onLinkClickStub);
  });
});