Sha256: 1a47436bdbf5f8d1eb38036650159ccf3af55fdb734a354a8a93f883368d9b38

Contents?: true

Size: 1.86 KB

Versions: 6

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"]
          }
        }.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

6 entries across 6 versions & 1 rubygems

Version Path
azeroth-0.6.1 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.6.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.5.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.4.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.3.0 spec/lib/azeroth/decorator/hash_builder_spec.rb
azeroth-0.2.0 spec/lib/azeroth/decorator/hash_builder_spec.rb