Skip to content

Commit

Permalink
add test and fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
Edge7481 committed Apr 18, 2024
1 parent 1e4a646 commit 3b847b6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
29 changes: 14 additions & 15 deletions app/assets/javascripts/osem-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,28 @@ $(document).ready( function() {
var now = new Date();
var closestEventId = null;
var smallestDiff = Infinity;
var i=0

$('.event-item').each(function() {

$('.date-content').each(function() {
var eventTimeStr = $(this).data('time');
if (eventTimeStr) {
var eventTimeParts = eventTimeStr.split(':');
var eventMinutes = parseInt(eventTimeParts[0]) * 60 + parseInt(eventTimeParts[1]);
var nowMinutes = now.getHours() * 60 + now.getMinutes();
var diff = Math.abs(eventMinutes - nowMinutes);

if (diff < smallestDiff) {
smallestDiff = diff;
closestEventId = $(this).find('.date-title').attr('id');
if (eventTimeStr) {
var eventTime = new Date(eventTimeStr);
var diff = Math.abs(eventTime - now);

if (diff < smallestDiff) {
smallestDiff = diff;
closestEventId = $(this).attr('class').split(' ')[1];
}
}
}
});

if (closestEventId) {
//Instead of relying on hash it's probably better to scroll using javascript
//Since the users and click button->scroll->click again, which won't re-scroll
var element = document.getElementById(closestEventId);
if (element) {
element.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
$('.highlighted').removeClass('highlighted');
$('.' + closestEventId).addClass('highlighted').get(0).scrollIntoView({ behavior: 'smooth', block: 'start' });
}
});

Expand Down
9 changes: 5 additions & 4 deletions app/views/schedules/events.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
- start_ymd = new_start_time.strftime('%Y-%m-%d')
- unless start_ymd.eql?(date)
.col-xs-12.col-md-12
.date-content{ data: { time: event_schedule.start_time.strftime('%H:%M') } }
%span.date-title{ id: "event-#{event_schedule.id}" }
.date-content
%span.date-title
= inyourtz(event_schedule.start_time, @conference.timezone) do
= date = start_ymd
%a.pull-right{ title: "Go up", href: "#program" }
Expand All @@ -64,7 +64,8 @@
= time + ' ' + timezone_text(tz_object)
.col-xs-12.col-md-11
- cache [@program, event_schedule, event_schedule.event, current_user, event_schedule.happening_now?, '#scheduled#full#panel'] do
= render 'event', event: event_schedule.event, event_schedule: event_schedule
.event-item{ data: { time: event_schedule.start_time.iso8601 }, class: "event-#{event_schedule.event.id}" }
= render 'event', event: event_schedule.event, event_schedule: event_schedule

/ confirmed events that are not scheduled
- if @unscheduled_events.any?
Expand All @@ -79,7 +80,7 @@
.unscheduled-event
- cache [@program, event, current_user, '#unscheduled#full#panel'] do
= render 'event', event: event, event_schedule: nil
%button#current-event-btn.btn.btn-primary{ type: "button" }
%button.btn.btn-primary#current-event-btn{ type: "button" }
Jump to Current Event

:javascript
Expand Down
46 changes: 46 additions & 0 deletions spec/features/event_schedules_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'spec_helper'

describe EventSchedule, js: true do
Timecop.return
let(:test_date) { Time.current }
let!(:conference) do
create(:full_conference, start_date: test_date - 1.hour, end_date: test_date + 5.days, start_hour: 0, end_hour: 24)
end
let!(:program) { conference.program }
let!(:selected_schedule) { create(:schedule, program: program) }
let!(:scheduled_event_early) do
program.update!(selected_schedule: selected_schedule)
create(:event, program: program, state: 'confirmed', abstract: '`markdown`')
end
let!(:event_schedule_early) do
create(:event_schedule, event: scheduled_event_early, schedule: selected_schedule,
start_time: test_date - 1.hours)
end
let!(:scheduled_event_mid) do
program.update!(selected_schedule: selected_schedule)
create(:event, program: program, state: 'confirmed')
end
let!(:event_schedule_mid) do
create(:event_schedule, event: scheduled_event_mid, schedule: selected_schedule,
start_time: test_date)
end
let!(:scheduled_event_late) do
program.update!(selected_schedule: selected_schedule)
create(:event, program: program, state: 'confirmed')
end
let!(:event_schedule_late) do
create(:event_schedule, event: scheduled_event_late, schedule: selected_schedule,
start_time: test_date + 1.hours)
end

before do
login_as(create(:user), scope: :user)
visit events_conference_schedule_path(conference_id: conference.short_title, favourites: false)
end

it 'jumps to the closest event' do
find('#current-event-btn').click
highlighted_element = page.find('.highlighted', visible: true, wait: 1)
expect(highlighted_element[:class]).to include("event-#{scheduled_event_mid.id}")
end
end

0 comments on commit 3b847b6

Please sign in to comment.