|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Spree::StateChangeTrackingJob, type: :job do |
| 6 | + let(:order) { create(:order, user: user) } |
| 7 | + let(:user) { create(:user) } |
| 8 | + let(:transition_timestamp) { Time.current } |
| 9 | + |
| 10 | + describe '#perform' do |
| 11 | + it 'creates a state change record with correct attributes' do |
| 12 | + expect { |
| 13 | + described_class.perform_now( |
| 14 | + order, |
| 15 | + 'cart', |
| 16 | + 'address', |
| 17 | + transition_timestamp |
| 18 | + ) |
| 19 | + }.to change(Spree::StateChange, :count).by(1) |
| 20 | + |
| 21 | + state_change = Spree::StateChange.last |
| 22 | + expect(state_change.previous_state).to eq('cart') |
| 23 | + expect(state_change.next_state).to eq('address') |
| 24 | + expect(state_change.name).to eq('order') |
| 25 | + expect(state_change.user_id).to eq(user.id) |
| 26 | + expect(state_change.stateful_id).to eq(order.id) |
| 27 | + expect(state_change.stateful_type).to eq('Spree::Order') |
| 28 | + expect(state_change.created_at).to be_within(1.second).of(transition_timestamp) |
| 29 | + expect(state_change.updated_at).to be_within(1.second).of(transition_timestamp) |
| 30 | + end |
| 31 | + |
| 32 | + it 'stores all state transitions in correct order' do |
| 33 | + transitions = [ |
| 34 | + ['cart', 'address'], |
| 35 | + ['address', 'delivery'], |
| 36 | + ['delivery', 'payment'], |
| 37 | + ['payment', 'confirm'], |
| 38 | + ['confirm', 'complete'], |
| 39 | + ['complete', 'canceled'], |
| 40 | + ['canceled', 'resumed'] |
| 41 | + ] |
| 42 | + |
| 43 | + transitions.each do |from_state, to_state| |
| 44 | + described_class.perform_now( |
| 45 | + order, |
| 46 | + from_state, |
| 47 | + to_state, |
| 48 | + transition_timestamp |
| 49 | + ) |
| 50 | + |
| 51 | + state_change = Spree::StateChange.last |
| 52 | + expect(state_change.previous_state).to eq(from_state) |
| 53 | + expect(state_change.next_state).to eq(to_state) |
| 54 | + expect(state_change.stateful_id).to eq(order.id) |
| 55 | + expect(state_change.stateful_type).to eq('Spree::Order') |
| 56 | + end |
| 57 | + |
| 58 | + expect(Spree::StateChange.count).to eq(transitions.length) |
| 59 | + expect(Spree::StateChange.order(:created_at).pluck(:previous_state, :next_state)).to eq(transitions) |
| 60 | + end |
| 61 | + |
| 62 | + it 'preserves the exact transition timestamp' do |
| 63 | + specific_time = Time.zone.parse('2023-12-25 10:30:45') |
| 64 | + |
| 65 | + described_class.perform_now( |
| 66 | + order, |
| 67 | + 'cart', |
| 68 | + 'address', |
| 69 | + specific_time |
| 70 | + ) |
| 71 | + |
| 72 | + state_change = Spree::StateChange.last |
| 73 | + expect(state_change.created_at).to eq(specific_time) |
| 74 | + expect(state_change.updated_at).to eq(specific_time) |
| 75 | + end |
| 76 | + |
| 77 | + context 'when the order has no user' do |
| 78 | + let(:order) { create(:order, user: nil) } |
| 79 | + |
| 80 | + it 'sets user_id to nil in the state change record' do |
| 81 | + described_class.perform_now( |
| 82 | + order, |
| 83 | + 'cart', |
| 84 | + 'address', |
| 85 | + transition_timestamp |
| 86 | + ) |
| 87 | + |
| 88 | + state_change = Spree::StateChange.last |
| 89 | + expect(state_change.user_id).to be_nil |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + context 'when the object has a order association' do |
| 94 | + let(:payment) { create(:payment, order: order) } |
| 95 | + |
| 96 | + it 'uses the order user_id if available' do |
| 97 | + described_class.perform_now( |
| 98 | + payment, |
| 99 | + 'checkout', |
| 100 | + 'completed', |
| 101 | + transition_timestamp |
| 102 | + ) |
| 103 | + |
| 104 | + state_change = Spree::StateChange.last |
| 105 | + expect(state_change.user_id).to eq(order.user_id) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + it 'stores stateful name' do |
| 110 | + described_class.perform_now( |
| 111 | + order, |
| 112 | + 'cart', |
| 113 | + 'address', |
| 114 | + transition_timestamp |
| 115 | + ) |
| 116 | + |
| 117 | + state_change = Spree::StateChange.last |
| 118 | + expect(state_change.name).to eq('order') |
| 119 | + end |
| 120 | + end |
| 121 | +end |
0 commit comments