-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathcode_field_spec.rb
134 lines (103 loc) · 3.63 KB
/
code_field_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
132
133
134
require "rails_helper"
RSpec.describe "CodeField", type: :system do
describe "without value" do
let!(:user) { create :user, custom_css: "" }
context "show" do
it "displays the projects empty custom_css (dash)" do
visit "/admin/resources/users/#{user.id}"
wait_for_loaded
expect(find_field_value_element("custom_css")).to have_text empty_dash
end
end
context "edit" do
it "has the projects custom_css label and empty code editor" do
visit "/admin/resources/users/#{user.id}/edit"
wait_for_loaded
custom_css_element = find_field_element("custom_css")
expect(custom_css_element).to have_text "CUSTOM CSS"
expect(custom_css_element).to have_css ".CodeMirror"
expect(page).to have_editor_display text: ""
end
it "change the projects custom_css code" do
visit "/admin/resources/users/#{user.id}/edit"
wait_for_loaded
fill_in_editor_field "Hello World"
save
expect(page).to have_editor_display text: "Hello World"
end
end
end
describe "with regular value" do
let!(:css) { ".input { background: #000000; }" }
let!(:user) { create :user, custom_css: css }
context "edit" do
it "has the projects custom_css label and filled code editor" do
visit "/admin/resources/users/#{user.id}/edit"
custom_css_element = find_field_element("custom_css")
expect(custom_css_element).to have_text "CUSTOM CSS"
expect(custom_css_element).to have_css ".CodeMirror"
expect(page).to have_editor_display text: css
end
it "change the projects custom_css code to another value" do
visit "/admin/resources/users/#{user.id}/edit"
wait_for_loaded
fill_in_editor_field ".input { background: #ffffff; }"
save
expect(page).to have_editor_display text: ".input { background: #ffffff; }"
end
end
context "show" do
it "displays the projects custom_css" do
visit "/admin/resources/users/#{user.id}"
wait_for_loaded
expect(page).to have_editor_display text: css
end
end
end
describe "with pretty_generated option for a JSON code field" do
let(:metadata) do
{
name: "New York",
country: "United States",
population: 8419600,
coordinates: {
latitude: 40.7128,
longitude: -74.006
},
timezone: "America/New_York",
climate: {
type: "humid subtropical",
average_temperature_celsius: 13.1
},
points_of_interest: [
"Statue of Liberty",
"Central Park",
"Empire State Building"
]
}
end
it "correctly formats JSON code on create / edit and displays it in a pretty way on the show page" do
visit "/admin/resources/cities/new"
wait_for_loaded
within find_field_element("metadata") do
fill_in_editor_field(JSON.pretty_generate(metadata))
end
save
json_text = page.evaluate_script('document.querySelector(".CodeMirror").CodeMirror.getValue()')
expect(JSON.parse(json_text, symbolize_names: true)).to eq(metadata)
click_on "Edit"
json_text = page.evaluate_script('document.querySelector(".CodeMirror").CodeMirror.getValue()')
expect(JSON.parse(json_text, symbolize_names: true)).to eq(metadata)
end
end
def fill_in_editor_field(text)
within ".CodeMirror" do
current_scope.click
type text
end
end
def have_editor_display(options)
editor_display_locator = ".CodeMirror-code"
have_css(editor_display_locator, **options)
end
end