2020-07-29 15:45:25 +02:00
|
|
|
|
describe Champs::PhoneChamp do
|
2024-04-01 07:16:32 +02:00
|
|
|
|
let(:champ) { build(:champ_phone) }
|
|
|
|
|
# subject { champ }
|
2021-10-25 17:18:38 +02:00
|
|
|
|
|
2024-04-01 07:16:32 +02:00
|
|
|
|
describe '#validate' do
|
2020-07-29 15:45:25 +02:00
|
|
|
|
it do
|
2024-04-01 07:16:32 +02:00
|
|
|
|
expect(champ_with_value(nil).validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0123456789 0123456789").validate(:champs_public_value)).to_not be_truthy
|
|
|
|
|
expect(champ_with_value("01.23.45.67.89 01.23.45.67.89").validate(:champs_public_value)).to_not be_truthy
|
|
|
|
|
expect(champ_with_value("3646").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0123456789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("01.23.45.67.89").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0123 45.67.89").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0033 123-456-789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0033 123-456-789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0033(0)123456789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+33-1.23.45.67.89").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+33 - 123 456 789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+33(0) 123 456 789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+33 (0)123 45 67 89").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+33 (0)1 2345-6789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+33(0) - 123456789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+1(0) - 123456789").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+49 2109 87654321").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("012345678").validate(:champs_public_value)).to be_truthy
|
2022-03-01 16:42:46 +01:00
|
|
|
|
# DROM numbers should be valid
|
2024-04-01 07:16:32 +02:00
|
|
|
|
expect(champ_with_value("06 96 04 78 07").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("05 94 22 31 31").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("+594 5 94 22 31 31").validate(:champs_public_value)).to be_truthy
|
2020-09-08 15:31:06 +02:00
|
|
|
|
# polynesian numbers should not return errors in any way
|
|
|
|
|
## landline numbers start with 40 or 45
|
2024-04-01 07:16:32 +02:00
|
|
|
|
expect(champ_with_value("45187272").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("40 473 500").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("40473500").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("45473500").validate(:champs_public_value)).to be_truthy
|
2020-09-08 15:31:06 +02:00
|
|
|
|
## +689 is the international indicator
|
2024-04-01 07:16:32 +02:00
|
|
|
|
expect(champ_with_value("+689 45473500").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("0145473500").validate(:champs_public_value)).to be_truthy
|
2020-09-08 15:31:06 +02:00
|
|
|
|
## polynesian mobile numbers start with 87, 88, 89
|
2024-04-01 07:16:32 +02:00
|
|
|
|
expect(champ_with_value("87473500").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("88473500").validate(:champs_public_value)).to be_truthy
|
|
|
|
|
expect(champ_with_value("89473500").validate(:champs_public_value)).to be_truthy
|
2021-10-25 17:18:38 +02:00
|
|
|
|
end
|
2022-03-01 16:44:16 +01:00
|
|
|
|
end
|
2021-10-25 17:18:38 +02:00
|
|
|
|
|
2022-03-01 16:44:16 +01:00
|
|
|
|
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
|
2020-07-29 15:45:25 +02:00
|
|
|
|
end
|
2022-03-01 16:45:44 +01:00
|
|
|
|
|
|
|
|
|
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
|
2020-07-29 15:45:25 +02:00
|
|
|
|
end
|
2022-03-01 16:44:16 +01:00
|
|
|
|
|
|
|
|
|
def champ_with_value(number)
|
2024-04-01 07:16:32 +02:00
|
|
|
|
champ.tap { |c| c.value = number }
|
2022-03-01 16:44:16 +01:00
|
|
|
|
end
|
2020-07-29 15:45:25 +02:00
|
|
|
|
end
|