-
Notifications
You must be signed in to change notification settings - Fork 84
/
array_type_spec.rb
103 lines (82 loc) · 2.51 KB
/
array_type_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
# frozen_string_literal: true
require "spec_helper"
RSpec.describe StoreModel::Types::ArrayType do
let(:type) { described_class.new(Configuration) }
let(:attributes_array) do
[
{
color: "red",
disabled_at: Time.new(2019, 2, 22, 12, 30)
},
{
color: "green",
disabled_at: Time.new(2019, 3, 12, 8, 10)
}
]
end
describe "#type" do
subject { type.type }
it { is_expected.to eq(:array) }
end
describe "#changed_in_place?" do
let(:configurations) do
attributes_array.map { |attributes| Configuration.new(attributes) }
end
it "marks object as changed" do
expect(type.changed_in_place?([], configurations)).to be_truthy
end
end
describe "#cast_value" do
subject { type.cast_value(value) }
shared_examples "cast examples" do
it { is_expected.to be_a(Array) }
it "assigns attributes" do
subject.zip(attributes_array).each do |config, config_attributes|
expect(config).to have_attributes(config_attributes)
end
end
end
context "when String is passed" do
let(:value) { ActiveSupport::JSON.encode(attributes_array) }
include_examples "cast examples"
end
context "when Array of hashes is passed" do
let(:value) { attributes_array }
include_examples "cast examples"
end
context "when Array of instances is passed" do
let(:value) { attributes_array.map { |attrs| Configuration.new(attrs) } }
include_examples "cast examples"
end
context "when nil is passed" do
let(:value) { nil }
it { is_expected.to be_nil }
end
context "when instance of illegal class is passed" do
let(:value) { {} }
it "raises exception" do
expect { type.cast_value(value) }.to raise_error(
StoreModel::Types::CastError,
"failed casting {}, only String or Array instances are allowed"
)
end
end
end
describe "#serialize" do
shared_examples "serialize examples" do
subject { type.serialize(value) }
it { is_expected.to be_a(String) }
it "is equal to attributes" do
expect(subject).to eq(ActiveSupport::JSON.encode(attributes_array))
end
end
context "when Array is passed" do
let(:value) { attributes_array }
include_examples "serialize examples"
end
context "when String is passed" do
let(:value) { ActiveSupport::JSON.encode(attributes_array) }
include_examples "serialize examples"
end
end
end