require "rails_helper" RSpec.describe Location, type: :model do describe "#new" do let(:location) { FactoryBot.build(:location) } before do stub_request(:get, /api.postcodes.io/) .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\",\"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) end it "belongs to an organisation" do expect(location.scheme).to be_a(Scheme) end it "infers the local authority" do location.postcode = "M1 1AE" location.save! expect(location.location_code).to eq("E08000003") end end describe "#validate_postcode" do let(:location) { FactoryBot.build(:location) } it "does not add an error if postcode is valid" do location.postcode = "M1 1AE" location.save! expect(location.errors).to be_empty end it "does add an error when the postcode is invalid" do location.postcode = "invalid" expect { location.save! } .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Postcode Enter a postcode in the correct format, for example AA1 1AA") end end describe "#units" do let(:location) { FactoryBot.build(:location) } it "does add an error when the postcode is invalid" do location.units = nil expect { location.save! } .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Units Enter total number of units at this location") end end describe "#type_of_unit" do let(:location) { FactoryBot.build(:location) } it "does add an error when the postcode is invalid" do location.type_of_unit = nil expect { location.save! } .to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Type of unit Select the most common type of unit at this location") end end describe "scopes" do before do FactoryBot.create(:location, name: "ABC", postcode: "NW18RR") FactoryBot.create(:location, name: "XYZ", postcode: "SE16HJ") end context "when searching by name" do it "returns case insensitive matching records" do expect(described_class.search_by_name("abc").count).to eq(1) expect(described_class.search_by_name("AbC").count).to eq(1) end end context "when searching by postcode" do it "returns case insensitive matching records" do expect(described_class.search_by_postcode("se1 6hj").count).to eq(1) expect(described_class.search_by_postcode("SE1 6HJ").count).to eq(1) end end context "when searching by all searchable field" do it "returns case insensitive matching records" do expect(described_class.search_by("aBc").count).to eq(1) expect(described_class.search_by("nw18rr").count).to eq(1) end end end end