From 4a728e1d3ec21f616b18c111f0dce41c204f2e05 Mon Sep 17 00:00:00 2001 From: Alexis Reigel Date: Sat, 31 Aug 2024 16:48:07 +0200 Subject: [PATCH 1/2] remove timecop gem Rails has long been including its own `ActiveSupport::Testing::TimeHelpers`, which makes the dependency on `timecop` obsolete. --- Gemfile | 4 -- Gemfile.lock | 2 - spec/features/edit_note_conflict_spec.rb | 66 ++++++++++++------------ spec/models/note_spec.rb | 18 ++++--- 4 files changed, 44 insertions(+), 46 deletions(-) diff --git a/Gemfile b/Gemfile index f685a6a7..8289a0f2 100644 --- a/Gemfile +++ b/Gemfile @@ -43,7 +43,3 @@ group :development do gem 'rubocop-rspec', '~> 2.19', require: false gem 'seed_box' end - -group :test do - gem 'timecop' -end diff --git a/Gemfile.lock b/Gemfile.lock index 5b1f1fd8..8264c613 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -370,7 +370,6 @@ GEM stringio (3.1.0) strscan (3.1.0) thor (1.3.1) - timecop (0.9.9) timeout (0.4.1) tzinfo (2.0.6) concurrent-ruby (~> 1.0) @@ -421,7 +420,6 @@ DEPENDENCIES selenium-webdriver spring spring-commands-rspec - timecop webrick BUNDLED WITH diff --git a/spec/features/edit_note_conflict_spec.rb b/spec/features/edit_note_conflict_spec.rb index 32f62ea0..6081e197 100644 --- a/spec/features/edit_note_conflict_spec.rb +++ b/spec/features/edit_note_conflict_spec.rb @@ -3,45 +3,47 @@ require 'rails_helper' RSpec.describe 'Edit note with conflict', :js do + include ActiveSupport::Testing::TimeHelpers + context 'as logged in user' do let(:user) { create_user } before { login_as user } it 'A copy of the note is created' do - Timecop.travel(Time.zone.local(2016, 8, 1, 15, 33)) do - note = Note.create!( - title: 'my note', - user: user, - content: 'note content', - uid: SecureRandom.uuid - ) - - # load the note... - visit_and_wait "/#/notes/#{note.uid}" - expect(page).to have_content 'my note' - expect(page).to have_content 'note content' - - # ...in the meantime there's an edit - note.update! content: '

note content - update 1

' - - find('.ql-editor').set('note content - update 2') - - after_save_cycle do - expect(page).to have_content 'SAVED' - end - - expect(page).to have_content 'my note (conflict 2016-08-01 15:33)' - - # the note has the updated content - expect(note.reload.content).to eq '

note content - update 2

' - - # the conflict copy has the old content - expect(Note.last).to have_attributes( - title: 'my note (conflict 2016-08-01 15:33)', - content: '

note content - update 1

' - ) + travel_to Time.zone.local(2016, 8, 1, 15, 33) + + note = Note.create!( + title: 'my note', + user: user, + content: 'note content', + uid: SecureRandom.uuid + ) + + # load the note... + visit_and_wait "/#/notes/#{note.uid}" + expect(page).to have_content 'my note' + expect(page).to have_content 'note content' + + # ...in the meantime there's an edit + note.update! content: '

note content - update 1

' + + find('.ql-editor').set('note content - update 2') + + after_save_cycle do + expect(page).to have_content 'SAVED' end + + expect(page).to have_content 'my note (conflict 2016-08-01 15:33)' + + # the note has the updated content + expect(note.reload.content).to eq '

note content - update 2

' + + # the conflict copy has the old content + expect(Note.last).to have_attributes( + title: 'my note (conflict 2016-08-01 15:33)', + content: '

note content - update 1

' + ) end end end diff --git a/spec/models/note_spec.rb b/spec/models/note_spec.rb index d2e0a760..b93854a8 100644 --- a/spec/models/note_spec.rb +++ b/spec/models/note_spec.rb @@ -3,6 +3,8 @@ require 'rails_helper' RSpec.describe Note do + include ActiveSupport::Testing::TimeHelpers + describe '#as_json' do context 'when no options provided' do it 'returns the whitelisted attributes' do @@ -120,16 +122,16 @@ it 'sets the archived_at timestamp' do now = Time.zone.local(2016, 8, 1, 15, 33) - Timecop.freeze(now) do - note = described_class.create!( - uid: SecureRandom.uuid, - user: create_user - ) + travel_to now + + note = described_class.create!( + uid: SecureRandom.uuid, + user: create_user + ) - note.archive! + note.archive! - expect(note.archived_at).to eq now - end + expect(note.archived_at).to eq now end end From ecf0bb3445b3c5c6a421ab1197856baf4f9e68ff Mon Sep 17 00:00:00 2001 From: Alexis Reigel Date: Sat, 31 Aug 2024 16:59:44 +0200 Subject: [PATCH 2/2] make the conflict spec less flaky The dependence on the time could have made the spec flaky (with timecop), as depending on the specs running speed the advancement in secods was not deterministic really. We now set explicit times when simulating the conflict. --- spec/features/edit_note_conflict_spec.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/features/edit_note_conflict_spec.rb b/spec/features/edit_note_conflict_spec.rb index 6081e197..6f0de77a 100644 --- a/spec/features/edit_note_conflict_spec.rb +++ b/spec/features/edit_note_conflict_spec.rb @@ -26,22 +26,24 @@ expect(page).to have_content 'note content' # ...in the meantime there's an edit + travel_to Time.zone.local(2016, 8, 1, 15, 34) note.update! content: '

note content - update 1

' + travel_to Time.zone.local(2016, 8, 1, 15, 35) find('.ql-editor').set('note content - update 2') after_save_cycle do expect(page).to have_content 'SAVED' end - expect(page).to have_content 'my note (conflict 2016-08-01 15:33)' + expect(page).to have_content 'my note (conflict 2016-08-01 15:35)' # the note has the updated content expect(note.reload.content).to eq '

note content - update 2

' # the conflict copy has the old content expect(Note.last).to have_attributes( - title: 'my note (conflict 2016-08-01 15:33)', + title: 'my note (conflict 2016-08-01 15:35)', content: '

note content - update 1

' ) end