demarches-normaliennes/spec/models/champs/phone_champ_spec.rb
Pierre de La Morinerie e32c9a9f94 models: don't attempt to format invalid phone numbers
This is a defensive-programming measure, because formatting an
invalid phone number may truncate some leading numbers.
2022-03-02 09:57:03 +00:00

66 lines
3 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

describe Champs::PhoneChamp do
let(:phone_champ) { build(:champ_phone) }
describe '#valid?' do
it do
expect(champ_with_value(nil)).to be_valid
expect(champ_with_value("0123456789 0123456789")).to_not be_valid
expect(champ_with_value("01.23.45.67.89 01.23.45.67.89")).to_not be_valid
expect(champ_with_value("3646")).to be_valid
expect(champ_with_value("0123456789")).to be_valid
expect(champ_with_value("01.23.45.67.89")).to be_valid
expect(champ_with_value("0123 45.67.89")).to be_valid
expect(champ_with_value("0033 123-456-789")).to be_valid
expect(champ_with_value("0033 123-456-789")).to be_valid
expect(champ_with_value("0033(0)123456789")).to be_valid
expect(champ_with_value("+33-1.23.45.67.89")).to be_valid
expect(champ_with_value("+33 - 123 456 789")).to be_valid
expect(champ_with_value("+33(0) 123 456 789")).to be_valid
expect(champ_with_value("+33 (0)123 45 67 89")).to be_valid
expect(champ_with_value("+33 (0)1 2345-6789")).to be_valid
expect(champ_with_value("+33(0) - 123456789")).to be_valid
expect(champ_with_value("+1(0) - 123456789")).to be_valid
expect(champ_with_value("+49 2109 87654321")).to be_valid
expect(champ_with_value("012345678")).to be_valid
# DROM numbers should be valid
expect(champ_with_value("06 96 04 78 07")).to be_valid
expect(champ_with_value("05 94 22 31 31")).to be_valid
expect(champ_with_value("+594 5 94 22 31 31")).to be_valid
# polynesian numbers should not return errors in any way
## landline numbers start with 40 or 45
expect(champ_with_value("45187272")).to be_valid
expect(champ_with_value("40 473 500")).to be_valid
expect(champ_with_value("40473500")).to be_valid
expect(champ_with_value("45473500")).to be_valid
## +689 is the international indicator
expect(champ_with_value("+689 45473500")).to be_valid
expect(champ_with_value("0145473500")).to be_valid
## polynesian mobile numbers start with 87, 88, 89
expect(champ_with_value("87473500")).to be_valid
expect(champ_with_value("88473500")).to be_valid
expect(champ_with_value("89473500")).to be_valid
end
end
describe '#to_s' do
context 'for valid phone numbers' do
it 'returns the national part of the number, formatted nicely' do
expect(champ_with_value("0115789055").to_s).to eq("01 15 78 90 55")
expect(champ_with_value("+33115789055").to_s).to eq("01 15 78 90 55")
# DROM phone numbers are formatted differently but still formatted
expect(champ_with_value("0696047807").to_s).to eq("0696 04 78 07")
expect(champ_with_value("45187272").to_s).to eq("45187272")
end
end
context 'for possible (but not valid) phone numbers' do
it 'returns the original' do
expect(champ_with_value("1234").to_s).to eq("1234")
end
end
end
def champ_with_value(number)
phone_champ.tap { |c| c.value = number }
end
end