2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe DraftSequence do
|
2019-05-06 22:12:20 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2017-09-15 03:44:04 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
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
|
2020-05-12 00:56:24 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should not produce next sequence for non-human user" do
|
|
|
|
user.id = -99_999
|
|
|
|
2.times { expect(DraftSequence.next!(user, "test")).to eq(0) }
|
2020-05-12 00:56:24 -05:00
|
|
|
end
|
2021-08-04 05:30:37 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "updates draft count" do
|
|
|
|
Draft.create!(user: user, draft_key: "test", data: {})
|
2021-08-04 05:30:37 -05:00
|
|
|
expect(user.reload.user_stat.draft_count).to eq(1)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(DraftSequence.next!(user, "test")).to eq 1
|
2021-08-04 05:30:37 -05:00
|
|
|
expect(user.reload.user_stat.draft_count).to eq(0)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe ".current" do
|
|
|
|
it "should return 0 by default" do
|
|
|
|
expect(DraftSequence.current(user, "test")).to eq 0
|
2017-09-15 03:44:04 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should return nil for non-human user" do
|
|
|
|
user.id = -99_999
|
|
|
|
expect(DraftSequence.current(user, "test")).to eq(0)
|
2020-05-12 00:56:24 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "should return the right sequence" do
|
|
|
|
expect(DraftSequence.next!(user, "test")).to eq(1)
|
|
|
|
expect(DraftSequence.current(user, "test")).to eq(1)
|
2017-09-15 03:44:04 -05:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|