UNPKG

2.56 kBJavaScriptView Raw
1// NB: Please note this test script is run serially (.serial)
2// This means they are supposed to be run in order, due to the
3// Get Models setting up context. It's setup this way for flexibility.
4import test from 'ava';
5import dotenv from 'dotenv';
6
7import { SquidexClientManager } from '../src/index';
8
9dotenv.config();
10
11const clientSecret = process.env.SQUIDEX_CLIENT_SECRET;
12const clientId = process.env.SQUIDEX_CLIENT_ID;
13const url = process.env.SQUIDEX_CONNECT_URL;
14const appName = process.env.APP_NAME;
15
16const client = new SquidexClientManager(url, appName, clientId, clientSecret);
17
18function uniqueString(prefix) {
19 const dateStamp = new Date().toString();
20 return prefix ? `${prefix}-${dateStamp}` : dateStamp;
21}
22
23async 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
33test.before('Get Models', async (t) => {
34 await client.ensureValidClient();
35 const models = client.Models();
36 t.true(Object.keys(models).length > 0);
37});
38
39test.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 // Make sure we can find the recently created article
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 // Update exiting article and check the changes were made
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 // Check update works via create or update call
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 // Clean up
73 const deleteOp = await client.DeleteAsync('Articles', { id: article.id });
74 t.true(deleteOp.status === 204);
75});
76
77test.serial('Tag', async (t) => {
78 await simpleWriteCheck(t, 'Tag', {
79 publish: true,
80 data: {
81 name: { iv: uniqueString('cool') },
82 },
83 });
84});