Sha256: 630c2a4b6b32953a228afb0ab5e26510db26ae764a135423f62ff62dc001a05b

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

require 'spec_helper'

describe Helena::Session do
  let(:survey) { create :survey }
  let(:version) { survey.versions.create }
  let(:session) { survey.sessions.create version: version }

  it { expect(subject).to belong_to(:survey) }
  it { expect(subject).to belong_to(:version) }

  it { expect(subject).to embed_many(:answers) }

  it { expect(subject).to validate_uniqueness_of(:token) }
  it { expect(subject).to validate_uniqueness_of(:view_token) }

  it { is_expected.to have_index_for(created_at: 1) }
  it { is_expected.to have_index_for(updated_at: 1) }

  it 'has a valid factory' do
    expect(build(:session)).to be_valid
  end

  it 'assigns a token after when creating a session' do
    expect_any_instance_of(Helena::Session).to receive(:generate_token).exactly(2).times.and_return('a493oP')
    expect(create(:session_without_token).token).to eq 'a493oP'
  end

  describe '#answers_as_yaml and #answer_as_yaml=' do
    before do
      session.answers << Helena::Answer.build_generic('c', 'Barbra Streisand!!', '192.999.0.1')
      session.answers << Helena::Answer.build_generic('a', 42, '192.168.0.1')
      session.answers << Helena::Answer.build_generic('b', true, '192.235.0.1')
    end

    it 'prints answers as yaml' do
      expect(session.answers_as_yaml).to eq "---\na: 42\nb: 'true'\nc: Barbra Streisand!!\n"
    end

    it 'updates a value by a given yaml' do
      expect do
        session.answers_as_yaml = 'a: 666'
        session.save
      end.to change { session.answers.find_by(code: 'a').value }.from(42).to(666)
    end

    it 'removes existings answers that is not in the yaml' do
      expect do
        session.answers_as_yaml = 'x: "test"'
        session.save
      end.to change { session.reload.answers.size }.by(-2)

      expect(session.answers.in(code: %w[a b c]).size).to eq 0
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
helena-2.1.0 spec/models/helena/session_spec.rb
helena-2.0.2 spec/models/helena/session_spec.rb
helena-2.0.1 spec/models/helena/session_spec.rb
helena-2.0.0 spec/models/helena/session_spec.rb