@pixwel/pixwel-sdk 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/.babelrc +2 -1
  2. package/README.md +60 -0
  3. package/coverage/clover.xml +581 -430
  4. package/coverage/coverage-final.json +15 -9
  5. package/coverage/lcov-report/Ass.js.html +212 -212
  6. package/coverage/lcov-report/Asset.js.html +27 -27
  7. package/coverage/lcov-report/AssetType.js.html +98 -0
  8. package/coverage/lcov-report/Encode.js.html +98 -0
  9. package/coverage/lcov-report/File.js.html +98 -0
  10. package/coverage/lcov-report/Filename.js.html +9 -726
  11. package/coverage/lcov-report/Ingest.js.html +1239 -279
  12. package/coverage/lcov-report/IngestFile.js.html +5 -5
  13. package/coverage/lcov-report/Model.js.html +821 -0
  14. package/coverage/lcov-report/Order.js.html +25 -25
  15. package/coverage/lcov-report/Srt.js.html +102 -102
  16. package/coverage/lcov-report/Sub.js.html +43 -43
  17. package/coverage/lcov-report/Tag.js.html +98 -0
  18. package/coverage/lcov-report/TimeFormat.js.html +63 -63
  19. package/coverage/lcov-report/fileType.js.html +188 -0
  20. package/coverage/lcov-report/index.html +164 -86
  21. package/coverage/lcov.info +1159 -846
  22. package/dist/index.js +74 -22
  23. package/dist/src/Asset.js +10 -5
  24. package/dist/src/AssetType.js +54 -0
  25. package/dist/src/Encode.js +54 -0
  26. package/dist/src/File.js +54 -0
  27. package/dist/src/Filename.js +7 -228
  28. package/dist/src/Ingest.js +365 -33
  29. package/dist/src/IngestFile.js +6 -2
  30. package/dist/src/Model.js +569 -0
  31. package/dist/src/Order.js +20 -9
  32. package/dist/src/Srt.js +5 -1
  33. package/dist/src/Tag.js +54 -0
  34. package/dist/src/fileType.js +43 -0
  35. package/index.js +14 -0
  36. package/package.json +9 -3
  37. package/src/AssetType.js +11 -0
  38. package/src/Encode.js +11 -0
  39. package/src/File.js +11 -0
  40. package/src/Filename.js +0 -239
  41. package/src/Ingest.js +347 -27
  42. package/src/Model.js +252 -0
  43. package/src/Tag.js +11 -0
  44. package/src/fileType.js +41 -0
  45. package/test/Filename.spec.js +3 -707
  46. package/test/Ingest.spec.js +1107 -152
  47. package/test/Model.spec.js +328 -0
@@ -0,0 +1,328 @@
1
+ import { Pixwel, Model } from "../index";
2
+ import axios from "axios";
3
+
4
+ let request, post, patch, get, deleteMock;
5
+
6
+ class AssetType extends Model {
7
+ constructor(data) {
8
+ super(data);
9
+ this._resource = "assettypes";
10
+ this._constraints = {
11
+ name: {
12
+ presence: true
13
+ }
14
+ };
15
+ }
16
+ static get _resource() {
17
+ return "assettypes";
18
+ }
19
+ }
20
+
21
+ describe("Model", () => {
22
+ beforeEach(() => {
23
+ request = jest.fn();
24
+ post = jest.fn().mockResolvedValue({
25
+ data: { _id: "a1b2c3", name: "dude", title: "Programmer" }
26
+ });
27
+ patch = jest.fn().mockResolvedValue({
28
+ data: { _id: "a1b2c3", name: "dude", title: "Programmer" }
29
+ });
30
+ get = jest.fn();
31
+ deleteMock = jest.fn();
32
+ axios.create = jest.fn().mockImplementation(() => {
33
+ return {
34
+ request: request,
35
+ post: post,
36
+ patch: patch,
37
+ get: get,
38
+ delete: deleteMock
39
+ };
40
+ });
41
+ });
42
+ describe("constructor", () => {
43
+ it("should assign data passed via params", () => {
44
+ let data = {
45
+ name: "dude"
46
+ };
47
+ let model = new Model(data);
48
+ expect(model.name).toEqual("dude");
49
+ });
50
+ it("should set up api connection", () => {
51
+ Pixwel.credentials.token = "a1b2c3";
52
+ let model = new Model();
53
+ expect(model._endpoint).toEqual("https://api.pixwel.com/api");
54
+ expect(axios.create.mock.calls[0][0]).toEqual({
55
+ baseURL: "https://api.pixwel.com/api",
56
+ headers: {
57
+ Authorization: "Basic a1b2c3",
58
+ Accept: "application/json",
59
+ "Content-Type": "application/json"
60
+ }
61
+ });
62
+ });
63
+ });
64
+ describe("set", () => {
65
+ it("should merge incoming data into the model", () => {
66
+ let model = new Model();
67
+ model.set({ details: { name: "Jeff" } });
68
+ expect(model.data()).toEqual({
69
+ _id: undefined,
70
+ details: { name: "Jeff" }
71
+ });
72
+ });
73
+ });
74
+ describe("validates", () => {
75
+ it("should return true if passes all constraints", () => {
76
+ class Person extends Model {
77
+ constructor(data) {
78
+ super(data);
79
+ this._constraints = {
80
+ name: {
81
+ presence: true
82
+ }
83
+ };
84
+ }
85
+ }
86
+ let person = new Person({ name: "Jeff" });
87
+ let isValid = person.validates();
88
+ expect(isValid).toBe(true);
89
+ });
90
+ it("should return false if fails a constraint", () => {
91
+ class Person extends Model {
92
+ constructor(data) {
93
+ super(data);
94
+ this._constraints = {
95
+ name: {
96
+ presence: true
97
+ }
98
+ };
99
+ }
100
+ }
101
+ let person = new Person();
102
+ let isValid = person.validates();
103
+ expect(isValid).toBe(false);
104
+ });
105
+ });
106
+ describe("errors", () => {
107
+ it("should returns empty array if all fields pass constraints", () => {
108
+ let res = new Model({ name: "Jeff" });
109
+ let isValid = res.validates();
110
+ expect(isValid).toBe(true);
111
+ let errors = res.errors();
112
+ expect(errors).toEqual([]);
113
+ });
114
+ it("should returns error array if a field fails a constraint", () => {
115
+ class Person extends Model {
116
+ constructor(data) {
117
+ super(data);
118
+ this._constraints = {
119
+ name: {
120
+ presence: true
121
+ }
122
+ };
123
+ }
124
+ }
125
+ let person = new Person();
126
+ let isValid = person.validates();
127
+ expect(isValid).toBe(false);
128
+ let errors = person.errors();
129
+ expect(errors).toEqual({ name: ["Name can't be blank"] });
130
+ });
131
+ });
132
+ describe("clone", () => {
133
+ it("should clone the model", () => {
134
+ let model = new Model({ name: "sup" });
135
+ let cloned = model.clone();
136
+ let res = cloned.save();
137
+ expect(res).toBeTruthy();
138
+ });
139
+ });
140
+ describe("registerCallback", () => {
141
+ it("register a listener on find with a callback", async done => {
142
+ get = jest.fn().mockResolvedValue({
143
+ data: [{ _id: "a1b2c3", name: "dude", title: "Programmer" }]
144
+ });
145
+ let callback = jest.fn();
146
+ Model.registerCallback("find", callback);
147
+ await Model.find();
148
+ expect(callback.mock.calls[0][0]).toEqual([
149
+ { _id: "a1b2c3", name: "dude", title: "Programmer" }
150
+ ]);
151
+ done();
152
+ });
153
+ it("register a listener on save with a callback", async done => {
154
+ get = jest.fn().mockResolvedValue({
155
+ data: [{ _id: "a1b2c3", name: "dude", title: "Programmer" }]
156
+ });
157
+ let callback = jest.fn();
158
+ Model.registerCallback("save", callback);
159
+ let model = new Model();
160
+ await model.save();
161
+ expect(callback.mock.calls[0][0]).toEqual({
162
+ _id: "a1b2c3",
163
+ name: "dude",
164
+ title: "Programmer"
165
+ });
166
+ done();
167
+ });
168
+ });
169
+ describe("all", () => {
170
+ it("should fetch an object collection with a object parameter", async done => {
171
+ get = jest.fn().mockResolvedValue({
172
+ data: { _id: "a1b2c3", name: "dude", title: "Programmer" }
173
+ });
174
+ let res = await AssetType.all({ verified: true });
175
+ expect(res).toBeTruthy();
176
+ expect(get.mock.calls[0][0]).toEqual("assettypes");
177
+ expect(get.mock.calls[0][1]).toEqual({
178
+ params: { verified: true }
179
+ });
180
+ done();
181
+ });
182
+ });
183
+ describe("find", () => {
184
+ it("should correctly set Model.resource when extended", async done => {
185
+ get = jest.fn().mockResolvedValue({
186
+ data: [{ _id: "a1b2c3", name: "dude", title: "Programmer" }]
187
+ });
188
+ let types = await AssetType.find();
189
+ expect(get.mock.calls).toEqual([["assettypes", { params: {} }]]);
190
+ done();
191
+ });
192
+ it("should fetch a single object with a string parameter", async done => {
193
+ get = jest.fn().mockResolvedValue({
194
+ data: { _id: "a1b2c3", name: "dude", title: "Programmer" }
195
+ });
196
+ let res = await AssetType.find("a1b2c3");
197
+ expect(res).toBeTruthy();
198
+ expect(get.mock.calls[0][0]).toEqual("assettypes/a1b2c3");
199
+ done();
200
+ });
201
+ it("should fetch an object collection with an empty parameter", async done => {
202
+ get = jest.fn().mockResolvedValue({
203
+ data: [{ _id: "a1b2c3", name: "dude", title: "Programmer" }]
204
+ });
205
+ let res = await AssetType.find();
206
+ expect(res).toBeTruthy();
207
+ expect(get.mock.calls[0][0]).toEqual("assettypes");
208
+ done();
209
+ });
210
+ it("should fetch an object collection with a object parameter", async done => {
211
+ get = jest.fn().mockResolvedValue({
212
+ data: [{ _id: "a1b2c3", name: "dude", title: "Programmer", verified: true }]
213
+ });
214
+ let res = await AssetType.find({ verified: true });
215
+ expect(res).toBeTruthy();
216
+ expect(get.mock.calls[0][0]).toEqual("assettypes");
217
+ expect(get.mock.calls[0][1]).toEqual({
218
+ params: { verified: true }
219
+ });
220
+ done();
221
+ });
222
+ });
223
+ describe("data", () => {
224
+ it("should return data values for model", () => {
225
+ let model = new Model({ name: "dude" });
226
+ model.age = 4;
227
+ let data = model.data();
228
+ expect(data).toEqual({ age: 4, name: "dude" });
229
+ });
230
+ });
231
+ describe("save", () => {
232
+ it("should patch differences on save if id is present", async done => {
233
+ let assetType = new AssetType({ name: "dude" });
234
+ await assetType.save();
235
+ assetType.age = 20;
236
+ let res = await assetType.save();
237
+ expect(patch.mock.calls[0][0]).toEqual(
238
+ "https://api.pixwel.com/api/assettypes/a1b2c3"
239
+ );
240
+ expect(patch.mock.calls[0][1]).toEqual({ age: 20 });
241
+ expect(res).toBeTruthy();
242
+ done();
243
+ });
244
+ it("should create a new record with object properties", async done => {
245
+ let assetType = new AssetType({ name: "dude" });
246
+ let res = await assetType.save();
247
+ expect(assetType.title).toEqual("Programmer");
248
+ expect(typeof res).toBe("object");
249
+ expect(post.mock.calls[0][0]).toEqual(
250
+ "https://api.pixwel.com/api/assettypes"
251
+ );
252
+ expect(post.mock.calls[0][1]).toEqual({ name: "dude" });
253
+ done();
254
+ });
255
+ it("should update a record with object properties when id is present", async done => {
256
+ let assetType = new AssetType({ _id: "a1b2c3", name: "dude" });
257
+ let res = await assetType.save();
258
+ expect(assetType.title).toEqual("Programmer");
259
+ expect(typeof res).toBe("object");
260
+ expect(patch.mock.calls[0][0]).toEqual(
261
+ "https://api.pixwel.com/api/assettypes/a1b2c3"
262
+ );
263
+ expect(patch.mock.calls[0][1]).toEqual({ _id: "a1b2c3", name: "dude" });
264
+ done();
265
+ });
266
+ });
267
+ describe("request", () => {
268
+ it("should make an api request", async done => {
269
+ request.mockResolvedValue("returnValue");
270
+ let model = new Model();
271
+ let res = await model.request({ hi: "dude" });
272
+ expect(res).toEqual("returnValue");
273
+ expect(request.mock.calls[0][0]).toEqual({ hi: "dude" });
274
+ done();
275
+ });
276
+ });
277
+ describe("create", () => {
278
+ it("should create a resource", async done => {
279
+ post.mockResolvedValue({ data: { name: "Thor" } });
280
+ let assetType = new AssetType();
281
+ let res = await assetType.create({ name: "Thor" });
282
+ expect(res).toEqual({ name: "Thor" });
283
+ expect(post.mock.calls[0][0]).toEqual(
284
+ "https://api.pixwel.com/api/assettypes"
285
+ );
286
+ expect(post.mock.calls[0][1]).toEqual({ name: "Thor" });
287
+ done();
288
+ });
289
+ });
290
+ describe("update", () => {
291
+ it("should update a resource", async done => {
292
+ patch.mockResolvedValue({ data: { _id: 'a1b2c3', name: "Thor" } });
293
+ let assetType = new AssetType();
294
+ let res = await assetType.update('a1b2c3', { name: "Thor" });
295
+ expect(res).toEqual({"_id": "a1b2c3", "name": "Thor"});
296
+ expect(patch.mock.calls[0][0]).toEqual(
297
+ "https://api.pixwel.com/api/assettypes/a1b2c3"
298
+ );
299
+ expect(patch.mock.calls[0][1]).toEqual({"name": "Thor"});
300
+ done();
301
+ });
302
+ });
303
+ describe("read", () => {
304
+ it("should read a resource using querystring", async done => {
305
+ get.mockResolvedValue({ data: [{ _id: 'a1b2c3', name: "Thor" }] });
306
+ let assetType = new AssetType();
307
+ let res = await assetType.read('a1b2c3' ,{ name: "Thor" });
308
+ expect(res).toEqual([{ _id: "a1b2c3", name: "Thor" }]);
309
+ expect(get.mock.calls[0][0]).toEqual(
310
+ "https://api.pixwel.com/api/assettypes/a1b2c3"
311
+ );
312
+ expect(get.mock.calls[0][1]).toEqual({ name: "Thor" });
313
+ done();
314
+ });
315
+ });
316
+ describe("delete", () => {
317
+ it("should delete a resource", async done => {
318
+ deleteMock.mockResolvedValue({ data: {} });
319
+ let assetType = new AssetType();
320
+ let res = await assetType.delete("a1b2c3");
321
+ expect(res).toEqual({});
322
+ expect(deleteMock.mock.calls[0][0]).toEqual(
323
+ "https://api.pixwel.com/api/assettypes/a1b2c3"
324
+ );
325
+ done();
326
+ });
327
+ });
328
+ });