|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative "../../app/lib/mavis_cli" |
| 4 | + |
| 5 | +describe "mavis clinics add-to-organisation" do |
| 6 | + context "when the organisation doesn't exist" do |
| 7 | + it "displays an error message" do |
| 8 | + when_i_run_the_command_expecting_an_error |
| 9 | + then_an_organisation_not_found_error_message_is_displayed |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + context "when the team doesn't exist" do |
| 14 | + it "displays an error message" do |
| 15 | + given_the_organisation_exists |
| 16 | + |
| 17 | + when_i_run_the_command_expecting_an_error |
| 18 | + then_a_team_not_found_error_message_is_displayed |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + context "when the clinic doesn't exist" do |
| 23 | + it "displays an error message" do |
| 24 | + given_the_organisation_exists |
| 25 | + and_the_team_exists |
| 26 | + |
| 27 | + when_i_run_the_command_expecting_an_error |
| 28 | + then_a_clinic_not_found_error_message_is_displayed |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context "when the clinic exists" do |
| 33 | + it "runs successfully" do |
| 34 | + given_the_organisation_exists |
| 35 | + and_the_team_exists |
| 36 | + and_the_clinic_exists |
| 37 | + |
| 38 | + when_i_run_the_command |
| 39 | + then_the_clinic_is_added_to_the_organisation |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + private |
| 44 | + |
| 45 | + def command |
| 46 | + Dry::CLI.new(MavisCLI).call( |
| 47 | + arguments: %w[clinics add-to-organisation ABC Team 123456] |
| 48 | + ) |
| 49 | + end |
| 50 | + |
| 51 | + def given_the_organisation_exists |
| 52 | + @organisation = create(:organisation, ods_code: "ABC") |
| 53 | + end |
| 54 | + |
| 55 | + def and_the_team_exists |
| 56 | + @team = create(:team, name: "Team", organisation: @organisation) |
| 57 | + end |
| 58 | + |
| 59 | + def and_the_clinic_exists |
| 60 | + @clinic = create(:community_clinic, name: "Clinic", ods_code: "123456") |
| 61 | + end |
| 62 | + |
| 63 | + def when_i_run_the_command |
| 64 | + @output = capture_output { command } |
| 65 | + end |
| 66 | + |
| 67 | + def when_i_run_the_command_expecting_an_error |
| 68 | + @output = capture_error { command } |
| 69 | + end |
| 70 | + |
| 71 | + def then_an_organisation_not_found_error_message_is_displayed |
| 72 | + expect(@output).to include("Could not find organisation.") |
| 73 | + end |
| 74 | + |
| 75 | + def then_a_team_not_found_error_message_is_displayed |
| 76 | + expect(@output).to include("Could not find team.") |
| 77 | + end |
| 78 | + |
| 79 | + def then_a_clinic_not_found_error_message_is_displayed |
| 80 | + expect(@output).to include("Could not find location: 123456") |
| 81 | + end |
| 82 | + |
| 83 | + def then_the_clinic_is_added_to_the_organisation |
| 84 | + expect(@organisation.community_clinics).to include(@clinic) |
| 85 | + end |
| 86 | +end |
0 commit comments