require 'spec_helper' describe LadyJosephine::Concerns::Pingable do let(:user) { User.create first_name: "Homer" } let(:article) { Article.create title: "Local man thinks wrestling is real" } it 'updates the ping if there is no user' do expect { expect(article.ping(user)).to be_truthy }.to change { article.lady_josephine_ping_user }.to(user) expect(article.lady_josephine_ping_user).to eql user end it 'updates the ping if the user is the same' do Timecop.freeze do article.lady_josephine_ping_user = user article.lady_josephine_ping_date = Time.now - 10.seconds expect { expect(article.ping(user)).to be_truthy }.to change { article.lady_josephine_ping_date }.to(Time.now) expect(article.lady_josephine_ping_user).to eql user end end it 'does not update the ping if the user is another one' do Timecop.freeze do article.lady_josephine_ping_user = user article.lady_josephine_ping_date = Time.now - 10.seconds other_user = User.create first_name: "Bart" expect { expect(article.ping(other_user)).to be_falsy }.not_to change { article.lady_josephine_ping_date } expect(article.lady_josephine_ping_user).to eql user end end it 'updates if the ping time is bigger than the PING_TIMESTAMP' do Timecop.freeze do article.lady_josephine_ping_user = user article.lady_josephine_ping_date = Time.now - 40.seconds other_user = User.create first_name: "Bart" expect { expect(article.ping(other_user)).to be_truthy }.to change { article.lady_josephine_ping_date }.to(Time.now) expect(article.lady_josephine_ping_user).to eql other_user end end it 'does not update if the ping time is smaller than the PING_TIMESTAMP' do Timecop.freeze do article.lady_josephine_ping_user = user article.lady_josephine_ping_date = Time.now - 10.seconds other_user = User.create first_name: "Bart" expect { expect(article.ping(other_user)).to be_falsy }.not_to change { article.lady_josephine_ping_date } expect(article.lady_josephine_ping_user).to eql user end end it 'does update if I force it to' do Timecop.freeze do article.lady_josephine_ping_user = user article.lady_josephine_ping_date = Time.now - 10.seconds other_user = User.create first_name: "Bart" expect { expect(article.ping(other_user, true)).to be_truthy }.to change { article.lady_josephine_ping_date }.to(Time.now) expect(article.lady_josephine_ping_user).to eql other_user end end end