Sha256: efbc28a2ce5a65204d365e57e2fc207577d44aa1a2364f9336cb841ab01c7a68

Contents?: true

Size: 1.86 KB

Versions: 16

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Azeroth::Decorator::HashBuilder do
  subject(:builder) { described_class.new(decorator) }

  let(:decorator_class) { DummyModel::Decorator }
  let(:decorator)       { decorator_class.new(model) }
  let(:model)           { build(:dummy_model) }

  describe '#as_json' do
    let(:expected_json) do
      {
        name: "#{model.first_name} #{model.last_name}",
        age: model.age,
        pokemon: model.favorite_pokemon
      }.stringify_keys
    end

    it 'returns meta data defined json' do
      expect(decorator.as_json).to eq(expected_json)
    end

    context 'when conditional attibute is exposed' do
      let(:model) { build(:dummy_model, first_name: nil) }

      let(:expected_json) do
        {
          name: model.last_name,
          age: model.age,
          pokemon: model.favorite_pokemon,
          errors: {
            first_name: ["can't be blank"]
          }
        }.deep_stringify_keys
      end

      it 'include the conditional attributes' do
        expect(decorator.as_json).to eq(expected_json)
      end
    end

    context 'when decorator has an expose block conditional' do
      let(:decorator_class) { Document::Decorator }
      let(:model)           { create(:document) }

      let(:expected_json) do
        {
          name: model.name
        }.stringify_keys
      end

      it 'returns meta data defined json' do
        expect(decorator.as_json).to eq(expected_json)
      end

      context 'when conditional returns true' do
        let(:model) { create(:document, reference: 'X-MAGIC-01') }

        let(:expected_json) do
          {
            name: model.name,
            reference: 'X-MAGIC-01'
          }.stringify_keys
        end

        it 'returns meta data defined json' do
          expect(decorator.as_json).to eq(expected_json)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
azeroth-1.0.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.10.1 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.10.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.9.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.8.2 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.8.1 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.8.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.7.4 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.7.3 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.7.2 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.7.1 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.7.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.6.5 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.6.4 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.6.3 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.6.2 spec/lib/azeroth/decorator/hash_builder_spec.rb