1 |
|
2 |
|
3 |
|
4 | import test from 'ava';
|
5 | import dotenv from 'dotenv';
|
6 |
|
7 | import { SquidexClientManager } from '../src/index';
|
8 |
|
9 | dotenv.config();
|
10 |
|
11 | const clientSecret = process.env.SQUIDEX_CLIENT_SECRET;
|
12 | const clientId = process.env.SQUIDEX_CLIENT_ID;
|
13 | const url = process.env.SQUIDEX_CONNECT_URL;
|
14 | const appName = process.env.APP_NAME;
|
15 |
|
16 | const client = new SquidexClientManager(url, appName, clientId, clientSecret);
|
17 |
|
18 | function uniqueString(prefix) {
|
19 | const dateStamp = new Date().toString();
|
20 | return prefix ? `${prefix}-${dateStamp}` : dateStamp;
|
21 | }
|
22 |
|
23 | async function simpleWriteCheck(t, modelName, payload) {
|
24 | const record = await client.CreateAsync(modelName, payload);
|
25 | t.truthy(record.id);
|
26 |
|
27 | const records = await client.RecordsAsync(modelName, { top: 0 });
|
28 | t.true(records.items.length > 0);
|
29 |
|
30 | await client.DeleteAsync(modelName, { id: record.id });
|
31 | }
|
32 |
|
33 | test.before('Get Models', async (t) => {
|
34 | await client.ensureValidClient();
|
35 | const models = client.Models();
|
36 | t.true(Object.keys(models).length > 0);
|
37 | });
|
38 |
|
39 | test.serial('Articles', async (t) => {
|
40 | const text = uniqueString('Testo');
|
41 | const expected = { data: { title: { iv: text } }, publish: true };
|
42 | const article = await client.CreateAsync('Articles', expected);
|
43 | t.truthy(article);
|
44 |
|
45 |
|
46 | const filter = await client.FilterRecordsAsync('Articles', expected, 'title');
|
47 | const created = filter[0];
|
48 | t.true(created !== undefined);
|
49 | t.true(created.data.title.iv === expected.data.title.iv);
|
50 |
|
51 |
|
52 | const update = await client.UpdateAsync('Articles', {
|
53 | id: article.id,
|
54 | data: {
|
55 | title: { iv: created.data.title.iv },
|
56 | text: { iv: 'x' },
|
57 | },
|
58 | });
|
59 | t.true(update.data.text.iv === 'x');
|
60 | t.truthy(article.id);
|
61 |
|
62 |
|
63 | const createOrUpdate = await client.CreateOrUpdateAsync('Articles', {
|
64 | id: article.id,
|
65 | data: {
|
66 | title: { iv: update.data.title.iv },
|
67 | text: { iv: 'y' },
|
68 | },
|
69 | }, 'title');
|
70 | t.true(createOrUpdate.data.text.iv === 'y');
|
71 |
|
72 |
|
73 | const deleteOp = await client.DeleteAsync('Articles', { id: article.id });
|
74 | t.true(deleteOp.status === 204);
|
75 | });
|
76 |
|
77 | test.serial('Tag', async (t) => {
|
78 | await simpleWriteCheck(t, 'Tag', {
|
79 | publish: true,
|
80 | data: {
|
81 | name: { iv: uniqueString('cool') },
|
82 | },
|
83 | });
|
84 | });
|