/
template_management_spec.rb
132 lines (109 loc) · 3.44 KB
/
template_management_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
121
122
123
124
125
126
127
128
129
130
131
require "rails_helper"
RSpec.describe "Template Management", :slow => true do
let(:admin) {FactoryGirl.create(:user, :admin)}
before do
as_user admin
navigate_to_template_admin_path
end
it "should show all the terms" do
click_link "Create New Template"
checkboxes = checkbox_states
expect(checkboxes.length).to eq(GenericAssetForm.terms.length)
GenericAssetForm.terms.each do |t|
expect(checkboxes[t]).not_to be_checked
end
end
context "when visiting the new template form" do
before do
click_link "Create New Template"
end
context "when saving a new template" do
let(:template_name) { "Test template %d" % Random.rand(99999) }
let(:check_fields) { [:title, :view, :subject] }
let(:caption) { "Available Templates" }
before do
fill_in "form_template_title", :with => template_name
check_fields.each {|field| show_field field}
require_field :title
click_button "Save Template"
end
it "should succeed" do
expect(page).to have_content("Created new template")
end
it "should show the template in the list" do
expect(page).to have_content(caption)
expect(page).to have_link(template_name)
end
context "when editing the template" do
before do
click_link "Edit " + template_name
end
it "should have persisted all fields" do
checkboxes = checkbox_states
check_fields.each {|field| expect(checkboxes[field]).to be_checked}
(GenericAssetForm.terms - check_fields).each do |t|
expect(checkboxes[t]).not_to be_checked
end
within "#title .form_template_properties_required" do
expect(find("input[type=checkbox]")).to be_checked
end
end
context "when submitting changes" do
let(:updated_template_name) { "Updated test template %d" % Random.rand(99999) }
before do
fill_in "form_template_title", :with => updated_template_name
show_field "creator"
hide_field "title"
click_button "Save Template"
end
it "should succeed" do
expect(page).to have_content("Template updated")
end
it "should show the template in the list" do
expect(page).to have_link(updated_template_name)
end
end
end
context "when deleting the template" do
before do
click_link "Delete " + template_name
end
it "should succeed" do
expect(page).to have_content("Template deleted")
end
it "should remove the template from the list" do
# We should have deleted the only template, so the whole table will
# be gone
expect(page).not_to have_content(caption)
end
end
end
end
end
def navigate_to_template_admin_path
visit root_path
click_link "Admin Panel"
click_link "Manage Ingest Templates"
end
def checkbox_states
checkboxes = HashWithIndifferentAccess.new
all('.template-property').each do |el|
checkboxes[el.find("legend").text] = el.find(".form_template_properties_visible input[type=checkbox]")
end
checkboxes
end
def show_field(field)
within "##{field}" do
check "Show"
end
end
def hide_field(field)
within "##{field}" do
uncheck "Show"
end
end
def require_field(field)
within "##{field}" do
check "Required"
end
end