Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for the examples from README.md. #986

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions test/readme-examples.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/* eslint-disable camelcase */
import addressFormatter from '../src/index';

describe('Examples from README.md', () => {
describe('Basic examples', () => {
it('first', () => {
const formatted = addressFormatter.format({
"houseNumber": 301,
"road": "Hamilton Avenue",
"neighbourhood": "Crescent Park",
"city": "Palo Alto",
"postcode": 94303,
"county": "Santa Clara County",
"state": "California",
"country": "United States of America",
"countryCode": "US",
});

expect(formatted).toBe(
`301 Hamilton Avenue
Palo Alto, CA 94303
United States of America
`);
});

it('second', () => {
const format = addressFormatter.format({
"houseNumber": 301,
"road": "Hamilton Avenue",
"neighbourhood": "Crescent Park",
"city": "Palo Alto",
"postcode": 94303,
"county": "Santa Clara County",
"state": "California",
"countryCode": "US",
}, {
appendCountry: true
});
expect(format).toBe(
`301 Hamilton Avenue
Palo Alto, CA 94303
United States of America
`
);
});
})

it('You can overwrite the country code incoming from the map service', () => {
const format = addressFormatter.format({
"houseNumber": 301,
"road": "Hamilton Avenue",
"neighbourhood": "Crescent Park",
"city": "Palo Alto",
"postcode": 94303,
"county": "Santa Clara County",
"state": "California",
"country": "United States of America",
"countryCode": "US",
}, {
abbreviate: true,
countryCode: 'UK'
});

expect(format).toBe(
`301 Hamilton Ave
Palo Alto 94303
USA
`
);
});

it('You can use a fallback to keep the library working when the country code is wrong', () => {
const format = addressFormatter.format({
"houseNumber": 301,
"road": "Hamilton Avenue",
"neighbourhood": "Crescent Park",
"city": "Palo Alto",
"postcode": 94303,
"county": "Santa Clara County",
"state": "California",
"country": "United States of America",
"countryCode": "yu",
}, {
abbreviate: true,
fallbackCountryCode: 'UK'
});
expect(format).toBe(
`301 Hamilton Ave
Palo Alto 94303
USA
`
);
});

it('You can get the address as a list of lines to make your formatting easier', () => {
const format = addressFormatter.format({
"houseNumber": 301,
"road": "Hamilton Avenue",
"neighbourhood": "Crescent Park",
"city": "Palo Alto",
"postcode": 94303,
"county": "Santa Clara County",
"state": "California",
"country": "United States of America",
"countryCode": "US",
}, {
output: 'array'
});

expect(format).toEqual([
'301 Hamilton Avenue',
'Palo Alto, CA 94303',
'United States of America'
]);
});
});