-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.externalize.test.js
136 lines (109 loc) · 4.44 KB
/
jquery.externalize.test.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* global require, global -- Globals defined by Node */
var test = require( "ava" );
var JSDOM = require( "jsdom" ).JSDOM;
var fixture = "<a href=\"https://example.com/\" title=\"example.com\">Go to example site</a>";
test.before( function() {
var dom = new JSDOM();
global.window = dom.window;
global.document = dom.window.document;
// add jQuery and plugin
global.$ = require( "jquery" );
require( "./jquery.externalize.js" );
} );
test( "Sets defaults as expected", function( t ) {
global.document.body.innerHTML = fixture;
// check default state
var link = global.document.querySelector( "a" );
t.is( link.target, "" );
t.is( link.rel, "" );
t.is( link.title, "example.com" );
// check state after plugin use
$( "a[href*='//']" ).externalize();
t.is( link.target, "_blank" );
t.is( link.rel, "external" );
t.is( link.title, "example.com (Opens in a new window)" );
} );
test( "Sets custom target window name when using the `name` property", function( t ) {
global.document.body.innerHTML = fixture;
// check default state
var link = global.document.querySelector( "a" );
t.is( link.target, "", "Target window name is empty." );
// check state after plugin use
$( "a[href*='//']" ).externalize( {
name: "_parent"
} );
t.is( link.target, "_parent", "Target window name is '_parent'." );
} );
test( "Sets event behavior when using the `target` property", function( t ) {
global.document.body.innerHTML = fixture;
// check default state
var link = global.document.querySelector( "a" );
t.is( link.target, "" );
// check state after plugin use
$( "a[href*='//']" ).externalize( {
target: false
} );
t.is( link.target, "" );
} );
test( "Sets a custom link relation when using the `relation` property", function( t ) {
global.document.body.innerHTML = fixture;
// check default state
var link = global.document.querySelector( "a" );
t.is( link.rel, "", "Link relationship is empty." );
// check state after plugin use
$( "a[href*='//']" ).externalize( {
relation: "nofollow"
} );
t.is( link.rel, "nofollow", "Link relationship is 'nofollow'." );
} );
test( "Sets a custom title when using the `title` property", function( t ) {
global.document.body.innerHTML = fixture;
// check default state
var link = global.document.querySelector( "a" );
t.is( link.title, "example.com", "Title is 'example.com'." );
// check state after plugin use
$( "a[href*='//']" ).externalize( {
title: "öffnet in neuem Fenster"
} );
t.is( link.title, "example.com (öffnet in neuem Fenster)",
"Title is 'example.com (öffnet in neuem Fenster)'." );
} );
test( "Sets the default title on elements without title attribute", function( t ) {
global.document.body.innerHTML = "<a href=\"https://example.com/\">Go to example site</a>";
// check default state
var link = global.document.querySelector( "a" );
t.is( link.title, "", "Title is empty." );
// check state after plugin use
$( "a[href*='//']" ).externalize();
t.is( link.title, "Opens in a new window",
"Title is 'Opens in a new window'." );
} );
test( "Appends the default relation on elements with a relation attribute", function( t ) {
global.document.body.innerHTML = "<a href=\"https://example.com/\" rel=\"help\">Go to example site</a>";
// check default state
var link = global.document.querySelector( "a" );
t.is( link.rel, "help", "Relation is 'help'." );
// check state after plugin use
$( "a[href*='//']" ).externalize();
t.is( link.rel, "help external",
"Relation is 'help external'." );
} );
test( "Handles only links and areas", function( t ) {
var markup = "";
markup += "<button class=\"test\">A button</button>";
markup += "<a href=\"https://example.com/\" class=\"test\">A link</a>";
markup += "<map name=\"foo\"><area class=\"test\" shape=\"rect\" coords=\"34,44,270,350\" alt=\"Foobar\" href=\"https://example.com/\"></map>";
global.document.body.innerHTML = markup;
// check default states
var button = global.document.querySelector( "button" );
t.is( button.rel, undefined, "Button relation does not exist." );
var link = global.document.querySelector( "a" );
t.is( link.rel, "", "Link relation is empty." );
var area = global.document.querySelector( "area" );
t.is( area.rel, "", "Area relation is empty." );
// check state after plugin use
$( ".test" ).externalize();
t.is( button.rel, undefined, "Button relation still does not exist." );
t.is( link.rel, "external", "Link relation is 'external'." );
t.is( area.rel, "external", "Area relation is 'external'." );
} );