Sha256: 80f6e938ff17091148501f50d70de7d209408bf0e039341552afb5e13b448773

Contents?: true

Size: 1.93 KB

Versions: 5

Compression:

Stored size: 1.93 KB

Contents

require 'spec_helper'

require_relative '../../../../lib/locomotive/steam/adapters/memory/order.rb'

describe Locomotive::Steam::Adapters::Memory::Order do

  let(:order) { Locomotive::Steam::Adapters::Memory::Order.new(*input) }

  describe '#list' do

    subject { order.list }

    let(:input) { nil }
    it { is_expected.to eq [] }

    context 'via a string' do

      let(:input) { 'name DESC' }
      it { is_expected.to eq [[:name, :desc]] }

    end

    context 'via a hash with symbol directions' do

      let(:input) { [{ name: :asc, date: :desc }] }
      it { is_expected.to eq [[:name, :asc], [:date, :desc]] }

    end

    context 'via a string' do

      let(:input) { 'name ASC, date DESC' }
      it { is_expected.to eq [[:name, :asc], [:date, :desc]] }

    end

  end

  describe '#apply_to' do

    subject { order.apply_to(entry, :en) }

    let(:input) { 'title asc, date desc' }
    let(:entry) { instance_double('Entry', title: 'foo', date: Time.zone.now) }
    it { expect(subject.map(&:class)).to eq([Locomotive::Steam::Adapters::Memory::Order::Asc, Locomotive::Steam::Adapters::Memory::Order::Desc]) }

    context 'localized field' do

      let(:now)   { Time.zone.now }
      let(:field) { instance_double('TitleI18nField', :[] => 'Hello world', translations: true) }
      let(:entry) { instance_double('Entry', title: field, date: now) }
      it { expect(subject.map(&:obj)).to eq(['Hello world', now]) }

    end

  end

  describe 'sort' do

    let(:array) {
      [
        instance_double('Entry1', id: 1, title: 'b', position: 1),
        instance_double('Entry2', id: 2, title: 'b', position: 2),
        instance_double('Entry3', id: 3, title: 'a', position: 3),
        instance_double('Entry3', id: 4, title: 'c', position: 1)
      ]
    }
    let(:input) { 'title asc, position desc' }

    subject { array.sort_by { |entry| order.apply_to(entry, :en) } }

    it { expect(subject.map(&:id)).to eq([3, 2, 1, 4]) }

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
locomotivecms_steam-1.0.1 spec/unit/adapters/memory/order_spec.rb
locomotivecms_steam-1.0.0 spec/unit/adapters/memory/order_spec.rb
locomotivecms_steam-1.0.0.rc10 spec/unit/adapters/memory/order_spec.rb
locomotivecms_steam-1.0.0.rc9 spec/unit/adapters/memory/order_spec.rb
locomotivecms_steam-1.0.0.rc8 spec/unit/adapters/memory/order_spec.rb