Skip to content

Commit

Permalink
add getZip method
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianosilvareis committed Apr 25, 2018
1 parent 3e79a9d commit 5240272
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"scripts": {
"clear": "rimraf lib && rimraf dist && rimraf coverage",
"build": "npm run clear && babel --out-dir lib src",
"build:watch": "npm run build -- --watch",
"build:umd": "./node_modules/.bin/webpack --output-filename viacep.umd.js --mode development",
"build:umd:min": "./node_modules/.bin/webpack --output-filename viacep.umd.min.js --mode production ",
"build:watch": "npm run build:umd -- --watch",
"build:all": "npm run build && npm run build:umd && npm run build:umd:min",
"lint": "./node_modules/.bin/eslint src/*.js",
"test": "jest",
Expand Down
1 change: 1 addition & 0 deletions src/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function searchZip(type, ...url) {

export default function address() {
return {
getZip: searchZip.bind(this, this.type),
getJson: searchZip.bind(this, 'json'),
getXml: searchZip.bind(this, 'xml'),
};
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { API_URL } from './config';
export default class ViaCep {
constructor(options) {
this.apiURL = options.apiURL || API_URL;
this.type = options.type || 'json';

this.zipCod = zipCod.bind(this)();
this.address = address.bind(this)();
Expand All @@ -17,6 +18,11 @@ export default class ViaCep {
request(url) {
this.url = url;

return fetch(url).then(data => data.json());
return fetch(url, {
mode: 'no-cors',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
}
}
1 change: 1 addition & 0 deletions src/zipCod.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function searchCod(type, cep) {

export default function zipCod() {
return {
getZip: searchCod.bind(this, this.type),
getJson: searchCod.bind(this, 'json'),
getXml: searchCod.bind(this, 'xml'),
getPiped: searchCod.bind(this, 'piped'),
Expand Down
44 changes: 43 additions & 1 deletion tests/address.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,56 @@ describe('address', () => {

describe('smoke tests', () => {
it('should address method is exist', () => {
expect(viacep.address).to.exist;
});

it('should getZip method in address is exist', () => {
expect(viacep.address.getZip).to.exist;
});

it('should getJson method in address is exist', () => {
expect(viacep.address.getJson).to.exist;
});

it('should address method is exist', () => {
it('should getXml method in address is exist', () => {
expect(viacep.address.getXml).to.exist;
});
});

describe('getZip', () => {
let viacepZipTest;

beforeEach(() => {
viacepZipTest = new ViaCep({
type: 'xml'
});
});

it('should call fetch method', () => {
viacepZipTest.address.getZip();
expect(stubedFetch).to.have.been.calledOnce;
});

it('should call fetch the correct URL', () => {
viacepZipTest.address
.getZip('RS', 'vitoria', 'adalberto')
expect(stubedFetch).to.have.be
.calledWith('https://viacep.com.br/ws/RS/vitoria/adalberto/xml/');

viacepZipTest.address
.getZip('uf', 'city', 'street')
expect(stubedFetch).to.have.be
.calledWith('https://viacep.com.br/ws/uf/city/street/xml/');
});

it('should return the correct data from the promise', () => {
promise.resolves(retorno);
const zipcods = viacepZipTest.address.getZip('es', 'vitoria', 'adalberto');
expect(zipcods.resolveValue).to.be.eql(retorno);
});

});

describe('getJson', () => {
it('should call fetch method', () => {
viacep.address.getJson();
Expand Down
35 changes: 35 additions & 0 deletions tests/zipcod.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ describe('zipCod', () => {
expect(viacep.zipCod).to.be.exist;
});

it('should getZip method exist', () => {
expect(viacep.zipCod.getZip).to.be.exist;
});

it('should getJson method exist', () => {
expect(viacep.zipCod.getJson).to.be.exist;
});
Expand All @@ -46,6 +50,37 @@ describe('zipCod', () => {
});
});

describe('getZip', () => {
let viacepZipTest;

beforeEach(() => {
viacepZipTest = new ViaCep({
type: 'piped'
});
})

it('should call fetch method', () => {
viacepZipTest.zipCod.getZip()
expect(stubedFetch).to.have.been.calledOnce;
});

it('should call fetch the correct URL', () => {
viacepZipTest.zipCod.getZip('29027422')
expect(stubedFetch).to.have.be
.calledWith('https://viacep.com.br/ws/29027422/piped/');

viacepZipTest.zipCod.getZip('29010250')
expect(stubedFetch).to.have.be
.calledWith('https://viacep.com.br/ws/29010250/piped/');
});

it('should return the correct data from the promise', () => {
promise.resolves({ endereco: 'rua alberto' });
const address = viacepZipTest.zipCod.getZip('29010250');
expect(address.resolveValue).to.be.eql({ endereco: 'rua alberto' });
});
});

describe('getJson', () => {
it('should call fetch method', () => {
viacep.zipCod.getJson()
Expand Down

0 comments on commit 5240272

Please sign in to comment.