From 5240272c8b7ecaa3b56d0cd4a305d8651f29782e Mon Sep 17 00:00:00 2001 From: Adriano Reis Date: Tue, 24 Apr 2018 22:27:13 -0300 Subject: [PATCH] add getZip method --- package.json | 2 +- src/address.js | 1 + src/index.js | 8 +++++++- src/zipCod.js | 1 + tests/address.spec.js | 44 ++++++++++++++++++++++++++++++++++++++++++- tests/zipcod.spec.js | 35 ++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 2d72f6e..fb2167a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/address.js b/src/address.js index a71aa01..e992b8c 100644 --- a/src/address.js +++ b/src/address.js @@ -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'), }; diff --git a/src/index.js b/src/index.js index 72d5dc2..79c2ca5 100644 --- a/src/index.js +++ b/src/index.js @@ -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)(); @@ -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', + }, + }); } } diff --git a/src/zipCod.js b/src/zipCod.js index 0719527..04dc6d0 100644 --- a/src/zipCod.js +++ b/src/zipCod.js @@ -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'), diff --git a/tests/address.spec.js b/tests/address.spec.js index bb8ae01..fc5ec27 100644 --- a/tests/address.spec.js +++ b/tests/address.spec.js @@ -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(); diff --git a/tests/zipcod.spec.js b/tests/zipcod.spec.js index d105048..e6d67a0 100644 --- a/tests/zipcod.spec.js +++ b/tests/zipcod.spec.js @@ -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; }); @@ -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()