-
Notifications
You must be signed in to change notification settings - Fork 75
/
test.js
149 lines (124 loc) · 3.9 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
139
140
141
142
143
144
145
146
147
148
149
"use strict";
import WooCommerceRestApi from "./index";
import nock from "nock";
describe("#options", () => {
test("wpAPIPrefix should set WP REST API custom path", () => {
const api = new WooCommerceRestApi({
url: "https://test.dev",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
wpAPIPrefix: "wp-rest",
version: "wc/v3"
});
const endpoint = "products";
const expected = "https://test.dev/wp-rest/wc/v3/" + endpoint;
const url = api._getUrl(endpoint);
expect(url).toBe(expected);
});
});
describe("#methods", () => {
const api = new WooCommerceRestApi({
url: "https://test.dev",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "wc/v3"
});
test("_getUrl should return full endpoint URL", () => {
const endpoint = "products";
const expected = "https://test.dev/wp-json/wc/v3/" + endpoint;
const url = api._getUrl(endpoint);
expect(url).toBe(expected);
});
test("_normalizeQueryString should return query string sorted by name", () => {
const url =
"http://test.dev/wp-json/wc/v3/products?filter[q]=Woo+Album&fields=id&filter[limit]=1";
const expected =
"http://test.dev/wp-json/wc/v3/products?fields=id&filter[limit]=1&filter[q]=Woo%20Album";
const normalized = api._normalizeQueryString(url);
expect(normalized).toBe(expected);
});
});
describe("#requests", () => {
beforeEach(() => {
nock.cleanAll();
});
const api = new WooCommerceRestApi({
url: "https://test.dev",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "wc/v3"
});
test("should return content for basic auth", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/wc/v3")
.post("/orders", {})
.reply(201, {
ok: true
});
return api.post("orders", {}).then(response => {
expect(response.status).toBe(201);
});
});
test("should return content for get requests", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/wc/v3")
.get("/orders")
.reply(200, {
ok: true
});
return api.get("orders", {}).then(response => {
expect(response.status).toBe(200);
});
});
test("should return content for put requests", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/wc/v3")
.put("/orders")
.reply(200, {
ok: true
});
return api.put("orders", {}).then(response => {
expect(response.status).toBe(200);
});
});
test("should return content for delete requests", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/wc/v3")
.delete("/orders")
.reply(200, {
ok: true
});
return api.delete("orders", {}).then(response => {
expect(response.status).toBe(200);
});
});
test("should return content for options requests", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/wc/v3")
.intercept("/orders", "OPTIONS")
.reply(200, {
ok: true
});
return api.options("orders", {}).then(response => {
expect(response.status).toBe(200);
});
});
test("should return content for OAuth", () => {
expect.assertions(1);
const oAuth = new WooCommerceRestApi({
url: "http://test.dev",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "wc/v3"
});
nock("http://test.dev/wp-json/wc/v3")
.filteringPath(/\?.*/, "?params")
.get("/orders?params")
.reply(200, {
ok: true
});
return oAuth.get("orders", {}).then(response => {
expect(response.status).toBe(200);
});
});
});