-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
138 lines (119 loc) · 4.03 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var WooCommerceAPI = require('./lib/react-native-woocommerce-api.js');
var chai = require('chai');
var nock = require('nock');
var url = '?consumer_key=ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&consumer_secret=cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
describe('#Construct', function() {
it('should throw an error if the url, consumerKey or consumerSecret are missing', function() {
chai.expect(function() {
new WooCommerceAPI();
}).to.throw(Error);
});
it('should set the default options', function() {
const api = new WooCommerceAPI({
url: 'https://yourstore.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
version: 'wc/v2',
queryStringAuth: true
});
chai.expect(api.version).to.equal('wc/v2');
chai.expect(api.isSsl).to.be.true;
chai.expect(api.verifySsl).to.be.true;
chai.expect(api.encoding).to.equal('utf8');
chai.expect(api.queryStringAuth).to.equal(true);
});
});
describe('#Requests', function() {
beforeEach(function() {
nock.cleanAll();
});
var api = new WooCommerceAPI({
url: 'https://yourstore.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
version: 'wc/v2',
queryStringAuth: true
});
it('should return full API url', function() {
var endpoint = 'products';
var expected = 'https://yourstore.dev/wp-json/wc/v2/products';
var url = api._getUrl(endpoint);
chai.assert.equal(url, expected);
});
it('should return full WP REST API url with a custom path', function() {
var restApi = new WooCommerceAPI({
url: 'https://yourstore.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
wpAPIPrefix: 'wp-rest',
version: 'wc/v2',
queryStringAuth: true
});
var endpoint = 'products';
var expected = 'https://yourstore.dev/wp-rest/wc/v2/products';
var url = restApi._getUrl(endpoint);
chai.assert.equal(url, expected);
});
it('should return content for basic auth', function(done) {
nock('https://yourstore.dev/wp-json/wc/v2')
.post('/orders'+url, {}).reply(200, {
ok: true
});
api.post('orders', {})
.then(data => {
chai.expect(data).be.a.string;
done();
});
//.catch(err => chai.expect(err).to.not.exist)
});
it('should return content for get requests', function(done) {
nock('https://yourstore.dev/wp-json/wc/v2')
.get('/orders'+url).reply(200, {
ok: true
});
api.get('orders')
.then(data => {
chai.expect(data).be.a.string;
done();
});
});
it('should return content for put requests', function(done) {
nock('https://yourstore.dev/wp-json/wc/v2')
.put('/orders'+url, {}).reply(200, {
ok: true
});
api.put('orders', {})
.then(data => {
chai.expect(data).be.a.string;
done();
});
});
it('should return content for delete requests', function(done) {
nock('https://yourstore.dev/wp-json/wc/v2')
.delete('/orders'+url).reply(200, {
ok: true
});
api.delete('orders')
.then(data => {
chai.expect(data).be.a.string;
done();
});
});
// it('should return content for options requests', function(done) {
// nock('https://yourstore.dev/wp-json/wc/v2')
// .intercept('/orders'+url, 'OPTIONS').reply(400);
// api.options('orders')
// .then(data => {
// chai.expect(data).be.a.string;
// done();
// })
// .catch(err => {
// console.log(err);
// chai.expect(err).to.not.exist;
// done();
// });
// });
});