forked from rubocop/rubocop
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_to_blank_spec.rb
120 lines (103 loc) · 4.27 KB
/
link_to_blank_spec.rb
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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Rails::LinkToBlank do
subject(:cop) { described_class.new }
context 'when not using target _blank' do
it 'does not register an offence' do
expect_no_offenses(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com'
RUBY
end
it 'does not register an offence when passing options' do
expect_no_offenses(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', class: 'big'
RUBY
end
it 'does not register an offence when using the block syntax' do
expect_no_offenses(<<-RUBY.strip_indent)
link_to 'https://www.example.com', class: 'big' do
"Click Here"
end
RUBY
end
end
context 'when using target_blank' do
context 'when using no rel' do
it 'registers and corrects an offence' do
expect_offense(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: '_blank'
^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener.
RUBY
expect_correction(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: '_blank', rel: 'noopener'
RUBY
end
it 'registers an offence when using a string for the target key' do
expect_offense(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', "target" => '_blank'
^^^^^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener.
RUBY
end
it 'registers an offence when using a symbol for the target value' do
expect_offense(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: :_blank
^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener.
RUBY
end
it 'registers an offence and auto-corrects when using the block syntax' do
expect_offense(<<-RUBY.strip_indent)
link_to 'https://www.example.com', target: '_blank' do
^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener.
"Click here"
end
RUBY
expect_correction(<<-RUBY.strip_indent)
link_to 'https://www.example.com', target: '_blank', rel: 'noopener' do
"Click here"
end
RUBY
end
it 'autocorrects with a new rel when using the block syntax ' \
'with parenthesis' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
link_to('https://www.example.com', target: '_blank') do
"Click here"
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
link_to('https://www.example.com', target: '_blank', rel: 'noopener') do
"Click here"
end
RUBY
end
it 'autocorrects with a new rel when using a symbol for the target \
value' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: :_blank
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: :_blank, rel: :noopener
RUBY
end
end
context 'when using rel' do
context 'when the rel does not contain noopener' do
it 'registers an offence and corrects' do
expect_offense(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', "target" => '_blank', rel: 'unrelated'
^^^^^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener.
RUBY
expect_correction(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', "target" => '_blank', rel: 'unrelated noopener'
RUBY
end
end
context 'when the rel contains noopener' do
it 'register no offence' do
expect_no_offenses(<<-RUBY.strip_indent)
link_to 'Click here', 'https://www.example.com', target: '_blank', rel: 'noopener noreferrer'
RUBY
end
end
end
end
end