This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
137 lines (110 loc) · 3.42 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
"use strict";
import CoCart from "./index";
import nock from "nock";
describe("#options", () => {
test("wpAPIPrefix should set WP REST API custom path", () => {
const api = new CoCart({
url: "https://test.dev",
wpAPIPrefix: "wp-rest",
version: "cocart/v2"
});
const endpoint = "products";
const expected = "https://test.dev/wp-rest/cocart/v2/" + endpoint;
const url = api._getUrl(endpoint);
expect(url).toBe(expected);
});
});
describe("#methods", () => {
const api = new CoCart({
url: "https://test.dev",
version: "cocart/v2"
});
test("_getUrl should return full endpoint URL", () => {
const endpoint = "products";
const expected = "https://test.dev/wp-json/cocart/v2/" + endpoint;
const url = api._getUrl(endpoint);
expect(url).toBe(expected);
});
test("_normalizeQueryString should return query string sorted by name", () => {
const url =
"https://test.dev/wp-json/cocart/v2/products?filter[q]=Woo+Album&fields=id&filter[limit]=1";
const expected =
"https://test.dev/wp-json/cocart/v2/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 CoCart({
url: "https://test.dev",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "cocart/v2"
});
test("should return content for basic auth", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/cocart/v2")
.post("/cart/add-item", {})
.reply(201, {
ok: true
});
return api.post("cart/add-item", {}).then(response => {
expect(response.status).toBe(201);
});
});
test("should return content for get requests", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/cocart/v2")
.get("/cart")
.reply(200, {
ok: true
});
return api.get("cart", {}).then(response => {
expect(response.status).toBe(200);
});
});
test("should return content for put requests", () => {
expect.assertions(1);
nock("https://test.dev/wp-json/cocart/v2")
.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/cocart/v2")
.delete("/cart/item/f28cd9f0560070670a861d86462b8f14")
.reply(200, {
ok: true
});
return api.delete("cart/item/f28cd9f0560070670a861d86462b8f14", {}).then(response => {
expect(response.status).toBe(200);
});
});
test("should return content for OAuth", () => {
expect.assertions(1);
const oAuth = new CoCart({
url: "https://test.dev",
consumerKey: "ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
consumerSecret: "cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
version: "cocart/v2",
oauth: true,
});
nock("https://test.dev/wp-json/cocart/v2")
.filteringPath(/\?.*/, "?params")
.get("/my-orders?params")
.reply(200, {
ok: true
});
return oAuth.get("my-orders", {}).then(response => {
expect(response.status).toBe(200);
});
});
});