require 'spec_helper' describe Spree::Store, type: :model do it { is_expected.to respond_to(:cart_tax_country_iso) } describe ".by_url" do let!(:store) { create(:store, url: "website1.com\nwww.subdomain.com") } let!(:store_2) { create(:store, url: 'freethewhales.com') } it "should find stores by url" do by_domain = Spree::Store.by_url('www.subdomain.com') expect(by_domain).to include(store) expect(by_domain).not_to include(store_2) end end describe '.current' do # there is a default store created with the test_app rake task. let!(:store_1) { Spree::Store.first || create(:store) } let!(:store_2) { create(:store, default: false, url: 'www.subdomain.com') } let!(:store_3) { create(:store, default: false, url: 'www.another.com', code: 'CODE') } it 'should return default when no domain' do expect(subject.class.current).to eql(store_1) end it 'should return store for domain' do expect(subject.class.current('spreecommerce.com')).to eql(store_1) expect(subject.class.current('www.subdomain.com')).to eql(store_2) end it 'should return store by code' do expect(subject.class.current('CODE')).to eql(store_3) end end describe ".default" do it "should ensure saved store becomes default if one doesn't exist yet" do expect(Spree::Store.where(default: true).count).to eq(0) store = build(:store) expect(store.default).not_to be true store.save! expect(store.default).to be true end it "should ensure there is only one default" do orig_default_store = create(:store, default: true) expect(orig_default_store.reload.default).to be true new_default_store = create(:store, default: true) expect(Spree::Store.where(default: true).count).to eq(1) [orig_default_store, new_default_store].each(&:reload) expect(new_default_store.default).to be true expect(orig_default_store.default).not_to be true end end describe '#default_cart_tax_location' do subject { described_class.new(cart_tax_country_iso: cart_tax_country_iso) } context "when there is no cart_tax_country_iso set" do let(:cart_tax_country_iso) { '' } it "responds with an empty default_cart_tax_location" do expect(subject.default_cart_tax_location).to be_empty end end context "when there is a cart_tax_country_iso set" do let(:country) { create(:country, iso: "DE") } let(:cart_tax_country_iso) { country.iso } it "responds with a default_cart_tax_location with that country" do expect(subject.default_cart_tax_location).to eq(Spree::Tax::TaxLocation.new(country: country)) end end end end