Browse Source
* create presenter class to hold all the db calls and data for showing the home page plus tests, some changes to factoreis and helpers necessary this should avoid an overly bloated controller method or calling the DB from a view helper * view partial changes to accommodate design changes, decouple box with counter and blue link boxes since various boxes show or not or in a different order depending on whether the org logs sales and whether we are in the crossover period I have also made grid and row partials to improve readability on the index page. * redesign the home page using the new presenter and partials, deliver the presenter object to the view from the controller * adjust tests and factories and address api call stub * remove view helpers that are no longer being used and remove outdated tests for home page * amend presenter and associated calls from view, tests, to ensure support user always sees sales data * write request specs for the homepage * undo unnecessary change to factory * make corrections following rebase conflicts * various minor changes following tech review * logs assigned to a provider should be vivisble to them in the homepage count rather than logs created by them * resolve rebase confusion in csv fixture filespull/2435/head v0.4.43
Arthur Campbell
8 months ago
committed by
GitHub
32 changed files with 888 additions and 504 deletions
@ -1,76 +0,0 @@
|
||||
module HomeHelper |
||||
def data_count(user, type) |
||||
lettings_years = FormHandler.instance.lettings_in_crossover_period? ? [current_collection_start_year, previous_collection_start_year] : [current_collection_start_year] |
||||
sales_years = FormHandler.instance.sales_in_crossover_period? ? [current_collection_start_year, previous_collection_start_year] : [current_collection_start_year] |
||||
|
||||
if user.data_provider? |
||||
case type |
||||
when "lettings" then user.lettings_logs.where(assigned_to: user).where(status: %i[in_progress]).filter_by_years(lettings_years).count |
||||
when "sales" then user.sales_logs.where(assigned_to: user).where(status: %i[in_progress]).filter_by_years(sales_years).count |
||||
when "misc" then user.lettings_logs.completed.where(assigned_to: user).count |
||||
end |
||||
else |
||||
case type |
||||
when "lettings" then user.lettings_logs.where(status: %i[in_progress]).filter_by_years(lettings_years).count |
||||
when "sales" then user.sales_logs.where(status: %i[in_progress]).filter_by_years(sales_years).count |
||||
when "schemes" then user.schemes.incomplete.count |
||||
end |
||||
end |
||||
end |
||||
|
||||
def heading_for_user_role(user) |
||||
ROLE_HEADINGS[user.role] |
||||
end |
||||
|
||||
def data_subheading(user, type) |
||||
case type |
||||
when "schemes" |
||||
"Incomplete schemes" |
||||
when "misc" |
||||
"Your completed lettings" |
||||
else |
||||
"#{user.role == 'data_provider' ? :"Your " : nil}#{type} in progress".capitalize |
||||
end |
||||
end |
||||
|
||||
def data_path(user, type) |
||||
lettings_years = FormHandler.instance.lettings_in_crossover_period? ? [current_collection_start_year, previous_collection_start_year] : [current_collection_start_year] |
||||
sales_years = FormHandler.instance.sales_in_crossover_period? ? [current_collection_start_year, previous_collection_start_year] : [current_collection_start_year] |
||||
if user.data_provider? |
||||
case type |
||||
when "lettings" then lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: lettings_years, owning_organisation_select: "all", managing_organisation_select: "all") |
||||
when "sales" then sales_logs_path(status: %i[in_progress], assigned_to: "you", years: sales_years, owning_organisation_select: "all", managing_organisation_select: "all") |
||||
when "misc" then lettings_logs_path(status: [:completed], assigned_to: "you", years: [""], owning_organisation_select: "all", managing_organisation_select: "all") |
||||
end |
||||
else |
||||
case type |
||||
when "lettings" then lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: lettings_years, owning_organisation_select: "all", managing_organisation_select: "all") |
||||
when "sales" then sales_logs_path(status: %i[in_progress], assigned_to: "all", years: sales_years, owning_organisation_select: "all", managing_organisation_select: "all") |
||||
when "schemes" then schemes_path(status: [:incomplete], owning_organisation_select: "all") |
||||
end |
||||
end |
||||
end |
||||
|
||||
def view_all_path(type) |
||||
case type |
||||
when "lettings" then clear_filters_path(filter_type: "lettings_logs") |
||||
when "sales" then clear_filters_path(filter_type: "sales_logs") |
||||
when "schemes" then clear_filters_path(filter_type: "schemes") |
||||
when "misc" then clear_filters_path(filter_type: "schemes") |
||||
end |
||||
end |
||||
|
||||
def view_all_text(type) |
||||
if type == "misc" |
||||
"View all schemes" |
||||
else |
||||
"View all #{type}" |
||||
end |
||||
end |
||||
|
||||
ROLE_HEADINGS = { |
||||
"data_provider" => "Complete your logs", |
||||
"data_coordinator" => "Manage your data", |
||||
"support" => "Manage all data", |
||||
}.freeze |
||||
end |
@ -0,0 +1,109 @@
|
||||
class HomepagePresenter |
||||
include Rails.application.routes.url_helpers |
||||
include CollectionTimeHelper |
||||
|
||||
attr_reader :current_year_in_progress_lettings_data, :current_year_completed_lettings_data, :current_year_in_progress_sales_data, :current_year_completed_sales_data, :last_year_in_progress_lettings_data, :last_year_completed_lettings_data, :last_year_in_progress_sales_data, :last_year_completed_sales_data, :incomplete_schemes_data |
||||
|
||||
def initialize(user) |
||||
@user = user |
||||
@display_sales = should_display_sales? |
||||
@in_crossover_period = FormHandler.instance.in_crossover_period? |
||||
@current_year = current_collection_start_year |
||||
@current_year_in_progress_lettings_data = data_box_data(:lettings, @current_year, :in_progress) |
||||
@current_year_completed_lettings_data = data_box_data(:lettings, @current_year, :completed) |
||||
@current_year_in_progress_sales_data = data_box_data(:sales, @current_year, :in_progress) if display_sales? |
||||
@current_year_completed_sales_data = data_box_data(:sales, @current_year, :completed) if display_sales? |
||||
@last_year = @current_year - 1 |
||||
@last_year_in_progress_lettings_data = data_box_data(:lettings, @last_year, :in_progress) if in_crossover_period? |
||||
@last_year_completed_lettings_data = data_box_data(:lettings, @last_year, :completed) |
||||
@last_year_in_progress_sales_data = data_box_data(:sales, @last_year, :in_progress) if in_crossover_period? && display_sales? |
||||
@last_year_completed_sales_data = data_box_data(:sales, @last_year, :completed) if display_sales? |
||||
if display_schemes? |
||||
@incomplete_schemes_data = { |
||||
count: @user.schemes.incomplete.count, |
||||
text: data_box_text(type: :schemes, status: :incomplete), |
||||
path: schemes_path(status: [:incomplete], owning_organisation_select: "all"), |
||||
} |
||||
end |
||||
end |
||||
|
||||
def title_text_for_user |
||||
if @user.support? |
||||
"Manage all data" |
||||
elsif @user.data_coordinator? |
||||
"Manage your organisation's logs" |
||||
else |
||||
"Manage logs assigned to you" |
||||
end |
||||
end |
||||
|
||||
def display_sales? |
||||
@display_sales |
||||
end |
||||
|
||||
def in_crossover_period? |
||||
@in_crossover_period |
||||
end |
||||
|
||||
def subheading_for_current_year |
||||
subheading_from_year @current_year |
||||
end |
||||
|
||||
def subheading_for_last_year |
||||
subheading = subheading_from_year @last_year |
||||
in_crossover_period? ? subheading : "#{subheading} (Closed collection year)" |
||||
end |
||||
|
||||
def display_schemes? |
||||
!@user.data_provider? |
||||
end |
||||
|
||||
private |
||||
|
||||
def subheading_from_year(year) |
||||
"#{year} to #{year + 1} Logs" |
||||
end |
||||
|
||||
def data_box_data(type, year, status) |
||||
{ |
||||
count: logs_count(type:, year:, status:), |
||||
text: data_box_text(type:, status:), |
||||
path: logs_link(type:, year:, status:), |
||||
} |
||||
end |
||||
|
||||
def data_box_text(type:, status:) |
||||
text = [status, type] |
||||
text.reverse! if status == :in_progress |
||||
text.join(" ").humanize |
||||
end |
||||
|
||||
def logs_link(type:, year:, status:) |
||||
params = { |
||||
status: [status], |
||||
years: [year], |
||||
assigned_to: @user.data_provider? ? "you" : "all", |
||||
owning_organisation_select: "all", |
||||
managing_organisation_select: "all", |
||||
} |
||||
case type |
||||
when :lettings then lettings_logs_path(params) |
||||
when :sales then sales_logs_path(params) |
||||
end |
||||
end |
||||
|
||||
def logs_count(type:, year:, status:) |
||||
query = case type |
||||
when :lettings then @user.lettings_logs |
||||
when :sales then @user.sales_logs |
||||
end |
||||
query = query.where(assigned_to: @user) if @user.data_provider? |
||||
query.filter_by_year(year) |
||||
.where(status:) |
||||
.count |
||||
end |
||||
|
||||
def should_display_sales? |
||||
@user.support? || @user.organisation.sales_logs.exists? |
||||
end |
||||
end |
@ -0,0 +1,3 @@
|
||||
<div class="app-data-box__lower"> |
||||
<%= govuk_link_to text, path, class: "govuk-link--inverse" %> |
||||
</div> |
@ -1,9 +1,6 @@
|
||||
<div class="app-data-box__upper"> |
||||
<div class="app-data-box__count"> |
||||
<%= govuk_link_to data_count(@current_user, type), data_path(@current_user, type), class: "govuk-link--no-visited-state govuk-link--no-underline" %> |
||||
<%= govuk_link_to data_box[:count], data_box[:path], class: "govuk-link--no-visited-state govuk-link--no-underline" %> |
||||
</div> |
||||
<%= govuk_link_to data_subheading(@current_user, type), data_path(@current_user, type), class: "govuk-link--no-visited-state" %> |
||||
</div> |
||||
<div class="app-data-box__lower"> |
||||
<%= govuk_link_to view_all_text(type), view_all_path(type), class: "govuk-link--inverse" %> |
||||
<%= govuk_link_to data_box[:text], data_box[:path], class: "govuk-link--no-visited-state" %> |
||||
</div> |
||||
|
@ -0,0 +1,8 @@
|
||||
<div class="app-data-box-one-half"> |
||||
<%= render partial: "home/data_box", object: left_data_box %> |
||||
</div> |
||||
<% if right_data_box %> |
||||
<div class="app-data-box-one-half"> |
||||
<%= render partial: "home/data_box", object: right_data_box %> |
||||
</div> |
||||
<% end %> |
@ -0,0 +1,8 @@
|
||||
<div class="app-data-box-one-half"> |
||||
<%= render partial: "home/data_box", object: top_left_data_box %> |
||||
<%= render partial: "home/data_box", object: bottom_left_data_box %> |
||||
</div> |
||||
<div class="app-data-box-one-half"> |
||||
<%= render partial: "home/data_box", object: top_right_data_box %> |
||||
<%= render partial: "home/data_box", object: bottom_right_data_box %> |
||||
</div> |
@ -1,332 +0,0 @@
|
||||
require "rails_helper" |
||||
require_relative "form/helpers" |
||||
|
||||
RSpec.describe "Home Page Features" do |
||||
include Helpers |
||||
|
||||
context "when there are notifications" do |
||||
let!(:user) { FactoryBot.create(:user) } |
||||
|
||||
context "when the notifications are currently active" do |
||||
before do |
||||
create(:notification, title: "Notification title 1") |
||||
create(:notification, title: "Notification title 2") |
||||
create(:notification, title: "Notification title 3") |
||||
sign_in user |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "shows the latest notification with count and dismiss link" do |
||||
expect(page).to have_content("Notification 1 of 3") |
||||
expect(page).to have_content("Notification title 3") |
||||
expect(page).to have_link("Dismiss") |
||||
expect(page).to have_link("Link text") |
||||
end |
||||
|
||||
context "when the user clicks a notification link" do |
||||
before do |
||||
click_link("Link text") |
||||
end |
||||
|
||||
it "takes them to the notification details page" do |
||||
expect(page).to have_current_path(notifications_path) |
||||
expect(page).to have_content("Notification title 3") |
||||
expect(page).to have_content("Some html content") |
||||
expect(page).to have_link("Back to Home") |
||||
end |
||||
|
||||
context "when they return" do |
||||
before do |
||||
click_link("Back to Home") |
||||
end |
||||
|
||||
it "the notification has not been dismissed" do |
||||
expect(page).to have_current_path(root_path) |
||||
expect(page).to have_content("Notification 1 of 3") |
||||
expect(page).to have_content("Notification title 3") |
||||
expect(page).to have_link("Dismiss") |
||||
expect(page).to have_link("Link text") |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the user clicks a dismiss link" do |
||||
before do |
||||
click_link("Dismiss") |
||||
end |
||||
|
||||
it "dismisses the notification and takes them back" do |
||||
expect(page).to have_current_path(root_path) |
||||
expect(page).to have_content("Notification 1 of 2") |
||||
expect(page).to have_content("Notification title 2") |
||||
expect(page).to have_link("Dismiss") |
||||
expect(page).to have_link("Link text") |
||||
end |
||||
|
||||
context "when the user dismisses the penultimate notification" do |
||||
before do |
||||
click_link("Dismiss") |
||||
end |
||||
|
||||
it "no longer displays the count" do |
||||
expect(page).to have_current_path(root_path) |
||||
expect(page).not_to have_content("Notification 1 of") |
||||
expect(page).to have_content("Notification title 1") |
||||
end |
||||
|
||||
context "when the user dismisses the final notification" do |
||||
before do |
||||
click_link("Dismiss") |
||||
end |
||||
|
||||
it "no longer displays any notification" do |
||||
expect(page).to have_current_path(root_path) |
||||
expect(page).not_to have_content("Notification") |
||||
expect(page).not_to have_link("Dismiss") |
||||
expect(page).not_to have_link("Link_text") |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when another user has dismissed all their notifications" do |
||||
before do |
||||
other_user = create(:user) |
||||
Notification.mark_as_read! :all, for: other_user |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "the first user can still see the notifications" do |
||||
expect(page).to have_content("Notification 1 of 3") |
||||
expect(page).to have_content("Notification title 3") |
||||
expect(page).to have_link("Dismiss") |
||||
expect(page).to have_link("Link text") |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the notifications are not currently active" do |
||||
before do |
||||
create(:notification, end_date: Time.zone.yesterday, title: "Notification title 1") |
||||
create(:notification, start_date: Time.zone.tomorrow, title: "Notification title 2") |
||||
sign_in user |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "does not show any notifications" do |
||||
expect(page).not_to have_content("Notification title") |
||||
expect(page).not_to have_content("Notification 1 of") |
||||
expect(page).not_to have_link("Dismiss") |
||||
expect(page).not_to have_link("Link text") |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the user is a data provider" do |
||||
let(:user) { FactoryBot.create(:user, name: "Provider") } |
||||
|
||||
before do |
||||
Timecop.freeze(Time.zone.local(2024, 1, 1)) |
||||
Singleton.__init__(FormHandler) |
||||
create_list(:lettings_log, 6, :in_progress, owning_organisation: user.organisation, assigned_to: user) |
||||
create_list(:lettings_log, 2, :in_progress, owning_organisation: user.organisation) |
||||
create_list(:lettings_log, 4, :completed, owning_organisation: user.organisation, assigned_to: user) |
||||
create_list(:lettings_log, 2, :completed) |
||||
create_list(:lettings_log, 2, :not_started) |
||||
sign_in user |
||||
visit(root_path) |
||||
end |
||||
|
||||
after do |
||||
Timecop.return |
||||
Singleton.__init__(FormHandler) |
||||
end |
||||
|
||||
it "displays the correct welcome text" do |
||||
expect(page).to have_current_path("/") |
||||
expect(page).to have_content("Welcome back, Provider") |
||||
expect(page).to have_content("Complete your logs") |
||||
end |
||||
|
||||
context "when their organisation has submitted sales logs" do |
||||
before do |
||||
create_list(:sales_log, 5, :in_progress, owning_organisation: user.organisation, assigned_to: user) |
||||
create_list(:sales_log, 3, :completed, owning_organisation: user.organisation, assigned_to: user) |
||||
create_list(:sales_log, 2, :not_started) |
||||
visit(root_path) |
||||
end |
||||
|
||||
context "and it is not a crossover" do |
||||
before do |
||||
closed_period_in_progress_log = build(:lettings_log, :in_progress, owning_organisation: user.organisation, assigned_to: user, startdate: Time.zone.local(2022, 4, 1)) |
||||
closed_period_in_progress_log.save!(validate: false) |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "displays correct data boxes, counts and links" do |
||||
data_boxes = page.find_all(class: "app-data-box-one-half") |
||||
expect(data_boxes.count).to eq(2) |
||||
expect(data_boxes[0].all("a").map(&:text)).to eq(["6", "Your lettings in progress", "View all lettings"]) |
||||
expect(data_boxes[0].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "lettings_logs")]) |
||||
expect(data_boxes[1].all("a").map(&:text)).to eq(["5", "Your sales in progress", "View all sales"]) |
||||
expect(data_boxes[1].all("a").map { |line| line["href"] }).to eq([sales_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), sales_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "sales_logs")]) |
||||
end |
||||
end |
||||
|
||||
context "and it is a crossover" do |
||||
before do |
||||
Timecop.freeze(Time.zone.local(2024, 4, 1)) |
||||
Singleton.__init__(FormHandler) |
||||
closed_period_in_progress_log = build(:lettings_log, :in_progress, owning_organisation: user.organisation, assigned_to: user, startdate: Time.zone.local(2022, 4, 1)) |
||||
closed_period_in_progress_log.save!(validate: false) |
||||
sign_in user |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "displays correct data boxes, counts and links" do |
||||
data_boxes = page.find_all(class: "app-data-box-one-half") |
||||
|
||||
expect(data_boxes.count).to eq(2) |
||||
expect(data_boxes[0].all("a").map(&:text)).to eq(["6", "Your lettings in progress", "View all lettings"]) |
||||
expect(data_boxes[0].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2024 2023], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2024 2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "lettings_logs")]) |
||||
expect(data_boxes[1].all("a").map(&:text)).to eq(["5", "Your sales in progress", "View all sales"]) |
||||
expect(data_boxes[1].all("a").map { |line| line["href"] }).to eq([sales_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2024 2023], owning_organisation_select: "all", managing_organisation_select: "all"), sales_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2024 2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "sales_logs")]) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when their organisation has never submitted sales logs" do |
||||
before do |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "displays correct data boxes, counts and links" do |
||||
data_boxes = page.find_all(class: "app-data-box-one-half") |
||||
expect(data_boxes.count).to eq(2) |
||||
expect(data_boxes[0].all("a").map(&:text)).to eq(["6", "Your lettings in progress", "View all lettings"]) |
||||
expect(data_boxes[0].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: %i[in_progress], assigned_to: "you", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "lettings_logs")]) |
||||
expect(data_boxes[1].all("a").map(&:text)).to eq(["4", "Your completed lettings", "View all schemes"]) |
||||
expect(data_boxes[1].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: [:completed], assigned_to: "you", years: [""], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: [:completed], assigned_to: "you", years: [""], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "schemes")]) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the user is a data coordinator" do |
||||
before do |
||||
Timecop.freeze(Time.zone.local(2024, 3, 1)) |
||||
Singleton.__init__(FormHandler) |
||||
create_list(:lettings_log, 6, :in_progress, owning_organisation: user.organisation) |
||||
create_list(:lettings_log, 2, :in_progress, owning_organisation: user.organisation, assigned_to: user) |
||||
create_list(:lettings_log, 4, :completed, owning_organisation: user.organisation) |
||||
create_list(:lettings_log, 2, :completed) |
||||
create_list(:lettings_log, 2, :not_started) |
||||
create_list(:scheme, 1, :incomplete, owning_organisation: user.organisation) |
||||
sign_in user |
||||
visit(root_path) |
||||
end |
||||
|
||||
after do |
||||
Timecop.return |
||||
Singleton.__init__(FormHandler) |
||||
end |
||||
|
||||
let(:user) { FactoryBot.create(:user, :data_coordinator, name: "Coordinator") } |
||||
|
||||
it "displays the correct welcome text" do |
||||
expect(page).to have_current_path("/") |
||||
expect(page).to have_content("Welcome back, Coordinator") |
||||
expect(page).to have_content("Manage your data") |
||||
end |
||||
|
||||
context "when their organisation has submitted sales logs" do |
||||
before do |
||||
create_list(:sales_log, 5, :in_progress, owning_organisation: user.organisation) |
||||
create_list(:sales_log, 3, :completed, owning_organisation: user.organisation) |
||||
create_list(:sales_log, 2, :not_started) |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "displays correct data boxes, counts and links" do |
||||
data_boxes = page.find_all(class: "app-data-box-one-third") |
||||
expect(data_boxes.count).to eq(3) |
||||
expect(data_boxes[0].all("a").map(&:text)).to eq(["8", "Lettings in progress", "View all lettings"]) |
||||
expect(data_boxes[0].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "lettings_logs")]) |
||||
expect(data_boxes[1].all("a").map(&:text)).to eq(["5", "Sales in progress", "View all sales"]) |
||||
expect(data_boxes[1].all("a").map { |line| line["href"] }).to eq([sales_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), sales_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "sales_logs")]) |
||||
expect(data_boxes[2].all("a").map(&:text)).to eq(["1", "Incomplete schemes", "View all schemes"]) |
||||
expect(data_boxes[2].all("a").map { |line| line["href"] }).to eq([schemes_path(status: [:incomplete], owning_organisation_select: "all"), schemes_path(status: [:incomplete], owning_organisation_select: "all"), clear_filters_path(filter_type: "schemes")]) |
||||
end |
||||
end |
||||
|
||||
context "when their organisation has never submitted sales logs" do |
||||
before do |
||||
visit(root_path) |
||||
end |
||||
|
||||
it "displays correct data boxes, counts and links" do |
||||
data_boxes = page.find_all(class: "app-data-box-one-half") |
||||
expect(data_boxes.count).to eq(2) |
||||
expect(data_boxes[0].all("a").map(&:text)).to eq(["8", "Lettings in progress", "View all lettings"]) |
||||
expect(data_boxes[0].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "lettings_logs")]) |
||||
expect(data_boxes[1].all("a").map(&:text)).to eq(["1", "Incomplete schemes", "View all schemes"]) |
||||
expect(data_boxes[1].all("a").map { |line| line["href"] }).to eq([schemes_path(status: [:incomplete], owning_organisation_select: "all"), schemes_path(status: [:incomplete], owning_organisation_select: "all"), clear_filters_path(filter_type: "schemes")]) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the user is a support user" do |
||||
let(:support_user) { FactoryBot.create(:user, :support, name: "Support") } |
||||
let(:notify_client) { instance_double(Notifications::Client) } |
||||
let(:confirmation_token) { "MCDH5y6Km-U7CFPgAMVS" } |
||||
let(:devise_notify_mailer) { DeviseNotifyMailer.new } |
||||
let(:otp) { "999111" } |
||||
|
||||
before do |
||||
Timecop.freeze(Time.zone.local(2024, 3, 1)) |
||||
Singleton.__init__(FormHandler) |
||||
create_list(:lettings_log, 2, :in_progress) |
||||
create_list(:lettings_log, 1, :completed) |
||||
create_list(:lettings_log, 2, :not_started) |
||||
create_list(:sales_log, 3, :in_progress) |
||||
create_list(:sales_log, 1, :completed) |
||||
create_list(:sales_log, 2, :not_started) |
||||
create_list(:scheme, 1, :incomplete) |
||||
completed_scheme = create(:scheme) |
||||
create(:location, scheme: completed_scheme) |
||||
allow(DeviseNotifyMailer).to receive(:new).and_return(devise_notify_mailer) |
||||
allow(devise_notify_mailer).to receive(:notify_client).and_return(notify_client) |
||||
allow(Devise).to receive(:friendly_token).and_return(confirmation_token) |
||||
allow(notify_client).to receive(:send_email).and_return(true) |
||||
allow(SecureRandom).to receive(:random_number).and_return(otp) |
||||
visit("/lettings-logs") |
||||
fill_in("user[email]", with: support_user.email) |
||||
fill_in("user[password]", with: support_user.password) |
||||
click_button("Sign in") |
||||
fill_in("code", with: otp) |
||||
click_button("Submit") |
||||
visit(root_path) |
||||
end |
||||
|
||||
after do |
||||
Timecop.return |
||||
Singleton.__init__(FormHandler) |
||||
end |
||||
|
||||
it "displays the correct welcome text" do |
||||
expect(page).to have_current_path("/") |
||||
expect(page).to have_content("Welcome back, Support") |
||||
expect(page).to have_content("Manage all data") |
||||
end |
||||
|
||||
it "displays correct data boxes, counts and links" do |
||||
data_boxes = page.find_all(class: "app-data-box-one-third") |
||||
expect(data_boxes.count).to eq(3) |
||||
expect(data_boxes[0].all("a").map(&:text)).to eq(["2", "Lettings in progress", "View all lettings"]) |
||||
expect(data_boxes[0].all("a").map { |line| line["href"] }).to eq([lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), lettings_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "lettings_logs")]) |
||||
expect(data_boxes[1].all("a").map(&:text)).to eq(["3", "Sales in progress", "View all sales"]) |
||||
expect(data_boxes[1].all("a").map { |line| line["href"] }).to eq([sales_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), sales_logs_path(status: %i[in_progress], assigned_to: "all", years: %w[2023], owning_organisation_select: "all", managing_organisation_select: "all"), clear_filters_path(filter_type: "sales_logs")]) |
||||
expect(data_boxes[2].all("a").map(&:text)).to eq(["1", "Incomplete schemes", "View all schemes"]) |
||||
expect(data_boxes[2].all("a").map { |line| line["href"] }).to eq([schemes_path(status: [:incomplete], owning_organisation_select: "all"), schemes_path(status: [:incomplete], owning_organisation_select: "all"), clear_filters_path(filter_type: "schemes")]) |
||||
end |
||||
end |
||||
end |
|
|
|
|
|
|
|
|
|
@ -0,0 +1,312 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe HomepagePresenter do |
||||
let(:organisation) { create(:organisation) } |
||||
let(:user) { create(:user, organisation:) } |
||||
let(:in_crossover_period) { true } |
||||
let(:presenter) { described_class.new(user) } |
||||
let(:date_this_year) { Time.zone.today } |
||||
let(:date_last_year) { Time.zone.today - 1.year } |
||||
let(:expected_count) { rand 1..10 } |
||||
|
||||
before do |
||||
allow(FormHandler.instance).to receive(:in_crossover_period?).and_return(in_crossover_period) |
||||
end |
||||
|
||||
context "when the user is support" do |
||||
let(:user) { create(:user, :support) } |
||||
|
||||
it "sets the correct title" do |
||||
expect(presenter.title_text_for_user).to eq "Manage all data" |
||||
end |
||||
|
||||
it "returns that schemes should be displayed" do |
||||
expect(presenter.display_schemes?).to be true |
||||
end |
||||
|
||||
it "returns that sales logs should be displayed" do |
||||
expect(presenter.display_sales?).to be true |
||||
end |
||||
end |
||||
|
||||
context "when the user is a data coordinator" do |
||||
let(:user) { create(:user, :data_coordinator) } |
||||
|
||||
it "sets the correct title" do |
||||
expect(presenter.title_text_for_user).to eq "Manage your organisation's logs" |
||||
end |
||||
|
||||
it "returns that schemes should be displayed" do |
||||
expect(presenter.display_schemes?).to be true |
||||
end |
||||
end |
||||
|
||||
context "when the user is a data provider" do |
||||
let(:user) { create(:user, :data_provider) } |
||||
|
||||
it "sets the correct title" do |
||||
expect(presenter.title_text_for_user).to eq "Manage logs assigned to you" |
||||
end |
||||
|
||||
it "returns that schemes should not be displayed" do |
||||
expect(presenter.display_schemes?).to be false |
||||
end |
||||
end |
||||
|
||||
context "when the user's organisation has never submitted sales logs" do |
||||
it "shows the user's organisation does not log sales" do |
||||
expect(presenter.display_sales?).to be false |
||||
end |
||||
|
||||
context "when in the crossover period" do |
||||
let(:in_crossover_period) { true } |
||||
|
||||
it "leaves all sales related data as nil" do |
||||
sales_data = [ |
||||
presenter.current_year_in_progress_sales_data, |
||||
presenter.current_year_completed_sales_data, |
||||
presenter.last_year_in_progress_sales_data, |
||||
presenter.last_year_completed_sales_data, |
||||
] |
||||
expect(sales_data).to all be nil |
||||
end |
||||
end |
||||
|
||||
context "when not in the crossover period" do |
||||
let(:in_crossover_period) { false } |
||||
|
||||
it "leaves all sales related data as nil" do |
||||
sales_data = [ |
||||
presenter.current_year_in_progress_sales_data, |
||||
presenter.current_year_completed_sales_data, |
||||
presenter.last_year_in_progress_sales_data, |
||||
presenter.last_year_completed_sales_data, |
||||
] |
||||
expect(sales_data).to all be nil |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the user's organisation has submitted sales logs" do |
||||
before do |
||||
create(:sales_log, assigned_to: user) |
||||
end |
||||
|
||||
it "shows the user's organisation logs sales" do |
||||
expect(presenter.display_sales?).to be true |
||||
end |
||||
|
||||
context "when in the crossover period" do |
||||
let(:in_crossover_period) { true } |
||||
|
||||
it "populates all sales related data" do |
||||
sales_data = [ |
||||
presenter.current_year_in_progress_sales_data, |
||||
presenter.current_year_completed_sales_data, |
||||
presenter.last_year_in_progress_sales_data, |
||||
presenter.last_year_completed_sales_data, |
||||
] |
||||
expect(sales_data).to all be_an_instance_of(Hash) |
||||
end |
||||
end |
||||
|
||||
context "when not in the crossover period" do |
||||
let(:in_crossover_period) { false } |
||||
|
||||
it "populates all relevant sales related data" do |
||||
sales_data = [ |
||||
presenter.current_year_in_progress_sales_data, |
||||
presenter.current_year_completed_sales_data, |
||||
presenter.last_year_completed_sales_data, |
||||
] |
||||
expect(sales_data).to all be_an_instance_of(Hash) |
||||
end |
||||
|
||||
it "does not populate data for last year's in progress logs" do |
||||
last_year_in_progress_data = [ |
||||
presenter.last_year_in_progress_lettings_data, |
||||
presenter.last_year_in_progress_sales_data, |
||||
] |
||||
expect(last_year_in_progress_data).to all be nil |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when in the crossover period" do |
||||
let(:in_crossover_period) { true } |
||||
|
||||
it "returns that we are in the crossover period" do |
||||
expect(presenter.in_crossover_period?).to be true |
||||
end |
||||
end |
||||
|
||||
context "when not in the crossover period" do |
||||
let(:in_crossover_period) { false } |
||||
|
||||
it "returns that we are in the crossover period" do |
||||
expect(presenter.in_crossover_period?).to be false |
||||
end |
||||
end |
||||
|
||||
describe "the data collected and exposed by the presenter" do |
||||
context "with lettings logs" do |
||||
let(:type) { :lettings_log } |
||||
|
||||
context "with in progress status" do |
||||
let(:status) { :in_progress } |
||||
|
||||
context "and the current year" do |
||||
let(:startdate) { date_this_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, status, assigned_to: user, startdate:) |
||||
data = presenter.current_year_in_progress_lettings_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Lettings in progress" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_this_year.year.to_s] |
||||
end |
||||
end |
||||
|
||||
context "and the last year" do |
||||
let(:startdate) { date_last_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, status, assigned_to: user, startdate:) |
||||
data = presenter.last_year_in_progress_lettings_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Lettings in progress" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_last_year.year.to_s] |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "with completed status" do |
||||
let(:status) { :completed } |
||||
|
||||
context "and the current year" do |
||||
let(:startdate) { date_this_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, :completed2024, assigned_to: user, startdate:) |
||||
data = presenter.current_year_completed_lettings_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Completed lettings" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_this_year.year.to_s] |
||||
end |
||||
end |
||||
|
||||
context "and the last year" do |
||||
let(:startdate) { date_last_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, status, assigned_to: user, startdate:) |
||||
data = presenter.last_year_completed_lettings_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Completed lettings" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_last_year.year.to_s] |
||||
end |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "with sales logs" do |
||||
let(:type) { :sales_log } |
||||
|
||||
context "with in progress status" do |
||||
let(:status) { :in_progress } |
||||
|
||||
context "and the current year" do |
||||
let(:saledate) { date_this_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, status, assigned_to: user, saledate:) |
||||
data = presenter.current_year_in_progress_sales_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Sales in progress" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_this_year.year.to_s] |
||||
end |
||||
end |
||||
|
||||
context "and the last year" do |
||||
let(:saledate) { date_last_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, status, assigned_to: user, saledate:) |
||||
data = presenter.last_year_in_progress_sales_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Sales in progress" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_last_year.year.to_s] |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "with completed status" do |
||||
let(:status) { :completed } |
||||
|
||||
context "and the current year" do |
||||
let(:saledate) { date_this_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, :completed2024, assigned_to: user, saledate:) |
||||
data = presenter.current_year_completed_sales_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Completed sales" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_this_year.year.to_s] |
||||
end |
||||
end |
||||
|
||||
context "and the last year" do |
||||
let(:saledate) { date_last_year } |
||||
|
||||
it "exposes the correct data for the data box" do |
||||
create_list(type, expected_count, status, assigned_to: user, saledate:) |
||||
data = presenter.last_year_completed_sales_data |
||||
|
||||
expect(data[:count]).to be expected_count |
||||
expect(data[:text]).to eq "Completed sales" |
||||
uri = URI.parse(data[:path]) |
||||
expect(uri.path).to eq "/#{type.to_s.dasherize}s" |
||||
query_params = CGI.parse(uri.query) |
||||
expect(query_params["status[]"]).to eq [status.to_s] |
||||
expect(query_params["years[]"]).to eq [date_last_year.year.to_s] |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue