-
Notifications
You must be signed in to change notification settings - Fork 53
/
linked-in.js
100 lines (81 loc) · 3.84 KB
/
linked-in.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* eslint-disable consistent-return, no-undef, no-unused-expressions */
import { parse } from "url";
import chai, { expect } from "chai";
import { stub, match } from "sinon";
import sinonChai from "sinon-chai";
import { JSDOM } from "jsdom";
import * as linkedInSharer from "../../src/sharers/linked-in";
chai.use(sinonChai);
describe("LinkedIn sharer", () => {
it("must have name 'linked-in'", () => {
expect(linkedInSharer.name).to.equal("linked-in");
});
it("must render a link to LinkedIn", () => {
const html = linkedInSharer.render("", "", "path/to/whatever");
const { window } = new JSDOM(html);
const anchor = window.document.querySelector("a[href^='https://www.linkedin.com/']");
expect(anchor).to.not.be.null;
});
describe("`getShareUrl` method", () => {
it("must have a `getShareUrl` helper method", () => {
expect(typeof linkedInSharer.getShareUrl).to.equal("function");
});
it("must have a `url` parameter in the sharing URL", () => {
const shareUrl = linkedInSharer.getShareUrl("path/to/whatever");
const parsed = parse(shareUrl, true);
expect(parsed.query).to.eql({ url: "path/to/whatever" });
});
});
describe("`action` method", () => {
it("must have a `action` method", () => {
expect(typeof linkedInSharer.action).to.equal("function");
});
it("must prevent the event's default", () => {
const html = linkedInSharer.render("", "", "path/to/whatever");
const { window } = new JSDOM(html);
const event = new window.Event("click");
const preventStub = stub(event, "preventDefault");
stub(window, "open").returns({});
linkedInSharer.action(event, window.document.body);
expect(preventStub.called).to.be.true;
});
it("must open a new window", () => {
const html = linkedInSharer.render("", "", "path/to/whatever");
const { window } = new JSDOM(html);
const event = new window.Event("click");
const openStub = stub(window, "open");
openStub.returns({});
linkedInSharer.action(event, window.document.body);
expect(openStub.calledOnce).to.be.true;
});
it("must open a new window named \"share_via_linked_in\"", () => {
const html = linkedInSharer.render("", "", "path/to/whatever");
const { window } = new JSDOM(html);
const event = new window.Event("click");
const openStub = stub(window, "open");
openStub.returns({});
linkedInSharer.action(event, window.document.body);
expect(openStub).to.have.been.calledWith(match.any, "share_via_linked_in", match.any);
});
it("must open a new window with the link provided by `getShareUrl`", () => {
const html = linkedInSharer.render("", "", "path/to/whatever");
const { window } = new JSDOM(html);
const event = new window.Event("click");
const openStub = stub(window, "open");
openStub.returns({});
const url = linkedInSharer.getShareUrl("path/to/whatever");
linkedInSharer.action(event, window.document.body);
expect(openStub).to.have.been.calledWith(url, match.any, match.any);
});
it("must nullify the popup's `opener` property", () => {
const html = linkedInSharer.render("", "", "path/to/whatever");
const { window } = new JSDOM(html);
const event = new window.Event("click");
const openStub = stub(window, "open");
const popup = {};
openStub.returns(popup);
linkedInSharer.action(event, window.document.body);
expect(popup.opener).to.be.null;
});
});
});