-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathingredient_picture_serializer_spec.rb
More file actions
185 lines (154 loc) · 4.57 KB
/
ingredient_picture_serializer_spec.rb
File metadata and controls
185 lines (154 loc) · 4.57 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true
require "rails_helper"
RSpec.describe Alchemy::JsonApi::IngredientPictureSerializer do
let(:ingredient) do
FactoryBot.build_stubbed(
:alchemy_ingredient_picture,
title: "Picture",
link: "/hello",
picture: picture,
)
end
let(:picture) { FactoryBot.create(:alchemy_picture, image_file_size: 301) }
subject(:serializer) { described_class.new(ingredient) }
it_behaves_like "an ingredient serializer"
describe ".preload_relations" do
subject { described_class.preload_relations }
it { is_expected.to eq([:thumbs]) }
end
describe "attributes" do
subject { serializer.serializable_hash[:data][:attributes] }
it "has the right keys and values" do
expect(subject[:title]).to eq("Picture")
expect(subject[:value]).to match(/pictures\/\w+\/image.png/)
expect(subject[:image_name]).to eq("image")
expect(subject[:image_file_name]).to eq("image.png")
expect(subject[:image_mime_type]).to eq("image/png")
expect(subject[:image_file_size]).to eq(301)
expect(subject[:image_dimensions]).to eq(width: 1, height: 1)
end
describe "image_dimensions" do
let(:image_dimensions) { subject[:image_dimensions] }
context "without image" do
let(:picture) { nil }
it { expect(image_dimensions).to be_nil }
end
context "with image" do
it do
expect(image_dimensions).to eq(width: 1, height: 1)
end
context "with ingredient settings[:size]" do
before do
expect(ingredient).to receive(:settings).at_least(:once) { size }
end
let(:size) do
{ size: "100x100" }
end
it do
expect(image_dimensions).to eq(width: 100, height: 100)
end
context "without y dimension" do
let(:size) do
{ size: "100x" }
end
it "infers height from ratio" do
expect(image_dimensions).to eq(width: 100, height: 100)
end
end
context "without x dimension" do
let(:size) do
{ size: "x50" }
end
it "infers width from ratio" do
expect(image_dimensions).to eq(width: 50, height: 50)
end
end
end
end
end
describe "srcset" do
let(:srcset) { subject[:srcset] }
context "without image" do
let(:picture) { nil }
it { expect(srcset).to be_nil }
end
context "with srcset defined" do
before do
expect(ingredient).to receive(:settings).at_least(:once) do
{
srcset: srcset_definition,
}
end
end
context "as strings" do
let(:srcset_definition) do
%w[100x100 200x100]
end
it "returns src sets objects" do
expect(srcset).to match_array(
[
{
url: instance_of(String),
desc: "100w",
width: "100",
height: "100",
type: "image/png",
},
{
url: instance_of(String),
desc: "200w",
width: "200",
height: "100",
type: "image/png",
},
]
)
end
end
context "as hash" do
let(:srcset_definition) do
[
{
size: "100x100",
crop: true,
},
{
size: "200x100",
format: "jpg",
},
]
end
it "returns src sets objects" do
expect(srcset).to match_array(
[
{
url: instance_of(String),
desc: "100w",
width: "100",
height: "100",
type: "image/png",
},
{
url: a_string_matching(%r{.jpg}),
desc: "200w",
width: "200",
height: "100",
type: "image/jpeg",
},
]
)
end
end
end
end
end
context "With no picture set" do
let(:picture) { nil }
describe "attributes" do
subject { serializer.serializable_hash[:data][:attributes] }
it "has the right keys and values" do
expect(subject[:value]).to be_nil
end
end
end
end