discourse/spec/models/draft_sequence_spec.rb
David Taylor c9dab6fd08
DEV: Automatically require 'rails_helper' in all specs (#16077)
It's very easy to forget to add `require 'rails_helper'` at the top of every core/plugin spec file, and omissions can cause some very confusing/sporadic errors.

By setting this flag in `.rspec`, we can remove the need for `require 'rails_helper'` entirely.
2022-03-01 17:50:50 +00:00

41 lines
1.2 KiB
Ruby

# frozen_string_literal: true
describe DraftSequence do
fab!(:user) { Fabricate(:user) }
describe '.next' do
it 'should produce next sequence for a key' do
expect(DraftSequence.next!(user, 'test')).to eq 1
expect(DraftSequence.next!(user, 'test')).to eq 2
end
it 'should not produce next sequence for non-human user' do
user.id = -99999
2.times { expect(DraftSequence.next!(user, 'test')).to eq(0) }
end
it 'updates draft count' do
Draft.create!(user: user, draft_key: 'test', data: {})
expect(user.reload.user_stat.draft_count).to eq(1)
expect(DraftSequence.next!(user, 'test')).to eq 1
expect(user.reload.user_stat.draft_count).to eq(0)
end
end
describe '.current' do
it 'should return 0 by default' do
expect(DraftSequence.current(user, 'test')).to eq 0
end
it 'should return nil for non-human user' do
user.id = -99999
expect(DraftSequence.current(user, 'test')).to eq(0)
end
it 'should return the right sequence' do
expect(DraftSequence.next!(user, 'test')).to eq(1)
expect(DraftSequence.current(user, 'test')).to eq(1)
end
end
end