2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-09-02 10:35:15 -04:00
|
|
|
require 'fast_spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe Serializers::JSON do
|
2018-09-02 10:35:15 -04:00
|
|
|
describe '.dump' do
|
|
|
|
let(:obj) { { key: "value" } }
|
|
|
|
|
|
|
|
subject { described_class.dump(obj) }
|
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
it 'returns a hash' do
|
|
|
|
is_expected.to eq(obj)
|
2018-09-02 10:35:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.load' do
|
|
|
|
let(:data_string) { '{"key":"value","variables":[{"key":"VAR1","value":"VALUE1"}]}' }
|
2020-04-30 20:09:59 -04:00
|
|
|
let(:data_hash) { Gitlab::Json.parse(data_string) }
|
2018-09-02 10:35:15 -04:00
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
context 'when loading a hash' do
|
|
|
|
subject { described_class.load(data_hash) }
|
|
|
|
|
|
|
|
it 'decodes a string' do
|
|
|
|
is_expected.to be_a(Hash)
|
|
|
|
end
|
|
|
|
|
2018-09-02 10:35:15 -04:00
|
|
|
it 'allows to access with symbols' do
|
|
|
|
expect(subject[:key]).to eq('value')
|
|
|
|
expect(subject[:variables].first[:key]).to eq('VAR1')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to access with strings' do
|
|
|
|
expect(subject["key"]).to eq('value')
|
|
|
|
expect(subject["variables"].first["key"]).to eq('VAR1')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
context 'when loading a nil' do
|
|
|
|
subject { described_class.load(nil) }
|
2018-09-02 10:35:15 -04:00
|
|
|
|
2019-06-13 09:12:28 -04:00
|
|
|
it 'returns nil' do
|
|
|
|
is_expected.to be_nil
|
2018-09-02 10:35:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|