Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinco committed Jan 15, 2025
1 parent 0eb5119 commit 1f1d0e9
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def set_resources
@product = current_facility.products.find_by!(
url_name: params[:service_id]
)
@sanger_product = @product.sanger_product || @product.create_sanger_product(group: "default")
@sanger_product = @product.sanger_product || @product.create_sanger_product
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Ability
def initialize(user, facility = nil)
return unless user

can [:show, :create, :update, :create_sample], Submission, user:
can [:show, :create, :update, :create_sample], Submission, user: user

if facility && user.operator_of?(facility)
can [:index, :show], Submission
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class SangerProduct < ApplicationRecord

belongs_to :product

attribute :group, default: 'default'

validates :group, presence: true, inclusion: { in: GROUPS }
validates :product, presence: true, uniqueness: { case_sensitive: false }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require "rails_helper"
require "controller_spec_helper"

RSpec.describe SangerSequencing::SangerProductsController do
let(:facility) { create(:setup_facility, sanger_sequencing_enabled: true) }
let(:service) { create(:setup_service, facility:, sanger_sequencing_enabled: true) }
let(:user) { create(:user, :facility_administrator, facility:) }

before do
sign_in user

@params = { facility_id: facility.url_name, service_id: service.url_name }
end

shared_examples "creates a sanger product if needed" do
it "creates a sanger product if needed" do
expect { do_request }.to(
change do
service.reload.sanger_product
end.from(nil).to(SangerSequencing::SangerProduct)
)
end
end

describe "show" do
before do
@method = :get
@action = :show
end

include_examples "creates a sanger product if needed"
end

describe "edit" do
before do
@method = :get
@action = :edit
end

include_examples "creates a sanger product if needed"
end

describe "update" do
before do
@method = :put
@action = :update
@params[:sanger_sequencing_sanger_product] = { group: "default" }
end

include_examples "creates a sanger product if needed"

it "updates the sanger product" do
sanger_product_json = {
"needs_primer" => true, "group" => "fragment"
}
@params[:sanger_sequencing_sanger_product] = sanger_product_json

service.create_sanger_product

expect { do_request }.to(
change do
service.reload.sanger_product.as_json(only: %i[needs_primer group])
end.from(
{ "needs_primer" => false, "group" => "default" }
).to(
sanger_product_json
)
)
end
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Sanger Products" do
let(:facility) { create(:setup_facility, sanger_sequencing_enabled: true) }
let(:service) { create(:setup_service, facility:, sanger_sequencing_enabled: true) }
let(:admin) { create(:user, :facility_administrator, facility:) }
let(:user) { create(:user) }

describe "admin tab" do
context "when not logged in" do
it "redirects to login" do
visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

expect(page).to have_content("Login")
expect(page).to_not have_content("Sanger")
expect(page).to_not have_content(service.name)
end
end

context "when logged in as normal user" do
before { login_as user }

it "renders permission denied" do
visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

expect(page).to have_content("Permission Denied")
end
end

context "when logged in as facility admin" do
before { login_as admin }

it "requires the facility to be sanger enabled" do
facility.update(sanger_sequencing_enabled: false)

visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

expect(page).to have_content("Not Found")
end

it "does not show the tab if service is sanger disabled" do
service.update(sanger_sequencing_enabled: false)

visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

within(".nav-tabs") do
expect(page).to_not have_content("Sanger")
end
end

it "shows the tab if service is sanger enabled" do
visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

within(".nav-tabs") do
expect(page).to have_content("Sanger")
end
end
end
end

describe "sanger product show" do
before { login_as admin }

it "can navigate by clicking the tab" do
visit manage_facility_service_path(facility, service)

within(".nav-tabs") do
click_link("Sanger")
end

expect(page).to have_content("Sanger Configuration")
end

it "shows sanger product information" do
visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

expect(page).to have_content("Sanger Configuration")
expect(page).to have_content("Needs a Primer")
expect(page).to have_content("Plated Service Type")
expect(page).to have_link("Edit")
end

it "allows to edit sanger product" do
visit facility_service_sanger_sequencing_sanger_product_path(facility, service)

click_link("Edit")

check("sanger_sequencing_sanger_product[needs_primer]")
select("Fragment Analysis", from: "sanger_sequencing_sanger_product[group]")

click_button("Save")

expect(page).to have_content("Sanger Configuration updated successfully")
end
end
end

0 comments on commit 1f1d0e9

Please sign in to comment.