|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var expect = require('expect.js'); |
| 4 | +var nock = require('nock'); |
| 5 | +var Q = require('q'); |
| 6 | + |
| 7 | +var client = require('../../../index'); |
| 8 | + |
| 9 | +describe('lib/resources/links', function() { |
| 10 | + |
| 11 | + before(function() { |
| 12 | + nock.disableNetConnect(); |
| 13 | + |
| 14 | + var config = { |
| 15 | + promise: function(resolver) { return Q.Promise(resolver); } |
| 16 | + }; |
| 17 | + |
| 18 | + this.callbackClient = client('sk-XXX').links; |
| 19 | + this.promiseClient = client('sk-XXX', config).links; |
| 20 | + }); |
| 21 | + |
| 22 | + after(function() { |
| 23 | + nock.enableNetConnect(); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('#create', function() { |
| 27 | + |
| 28 | + beforeEach(function() { |
| 29 | + this.url = 'https://www.jet.com/'; |
| 30 | + this.experience = { |
| 31 | + btn_pub_ref: 'my-pub-ref', |
| 32 | + btn_pub_user: 'user-id' |
| 33 | + }; |
| 34 | + |
| 35 | + this.payload = { |
| 36 | + url: this.url, |
| 37 | + experience: this.experience |
| 38 | + }; |
| 39 | + |
| 40 | + this.link = { |
| 41 | + merchant_id: 'org-XXX', |
| 42 | + affiliate: null, |
| 43 | + links: { |
| 44 | + universal: 'https://r.bttn.io?btn_pub_ref=my-pub-ref&btn_pub_user=user-id&btn_url=https%3A%2F%2Fwww.jet.com&btn_ref=org-XXX' |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + this.scope = nock('https://api.usebutton.com:443') |
| 49 | + .post('/v1/links', this.payload) |
| 50 | + .reply(200, { meta: { status: 'ok' }, 'object': this.link }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('creates a link with a callback', function(done) { |
| 54 | + this.callbackClient.create(this.payload, function(err, res) { |
| 55 | + expect(err).to.be(null); |
| 56 | + expect(res.data).to.eql(this.link); |
| 57 | + this.scope.done(); |
| 58 | + done(); |
| 59 | + }.bind(this)); |
| 60 | + }); |
| 61 | + |
| 62 | + it('creates a link with a promise', function(done) { |
| 63 | + this.promiseClient.create(this.payload).then(function(result) { |
| 64 | + expect(result.data).to.eql(this.link); |
| 65 | + this.scope.done(); |
| 66 | + done(); |
| 67 | + }.bind(this)).catch(done); |
| 68 | + }); |
| 69 | + |
| 70 | + }); |
| 71 | + |
| 72 | + describe('#getInfo', function() { |
| 73 | + |
| 74 | + beforeEach(function() { |
| 75 | + this.url = 'https://www.jet.com/'; |
| 76 | + |
| 77 | + this.payload = { |
| 78 | + url: this.url |
| 79 | + }; |
| 80 | + |
| 81 | + this.link = { |
| 82 | + organization_id: 'org-XXX', |
| 83 | + approved: true, |
| 84 | + ios_support: { |
| 85 | + app_to_web: true, |
| 86 | + app_to_app: true, |
| 87 | + web_to_web: true, |
| 88 | + web_to_app: false, |
| 89 | + web_to_app_with_install: false |
| 90 | + }, |
| 91 | + android_support: { |
| 92 | + app_to_web: true, |
| 93 | + app_to_app: true, |
| 94 | + web_to_web: true, |
| 95 | + web_to_app: false, |
| 96 | + web_to_app_with_install: false |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + this.scope = nock('https://api.usebutton.com:443') |
| 101 | + .post('/v1/links/info', this.payload) |
| 102 | + .reply(200, { meta: { status: 'ok' }, 'object': this.link }); |
| 103 | + }); |
| 104 | + |
| 105 | + it('gets information for a link with a callback', function(done) { |
| 106 | + this.callbackClient.getInfo(this.payload, function(err, res) { |
| 107 | + expect(err).to.be(null); |
| 108 | + expect(res.data).to.eql(this.link); |
| 109 | + this.scope.done(); |
| 110 | + done(); |
| 111 | + }.bind(this)); |
| 112 | + }); |
| 113 | + |
| 114 | + it('gets information for a link with a promise', function(done) { |
| 115 | + this.promiseClient.getInfo(this.payload).then(function(result) { |
| 116 | + expect(result.data).to.eql(this.link); |
| 117 | + this.scope.done(); |
| 118 | + done(); |
| 119 | + }.bind(this)).catch(done); |
| 120 | + }); |
| 121 | + |
| 122 | + }); |
| 123 | + |
| 124 | +}); |
0 commit comments