UNPKG

2.76 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 path from 'path';
5
6import test from 'ava';
7import dotenv from 'dotenv';
8
9import { SquidexClientManager } from '../src/index';
10
11dotenv.config();
12
13const clientSecret = process.env.SQUIDEX_CLIENT_SECRET;
14const clientId = process.env.SQUIDEX_CLIENT_ID;
15const url = process.env.SQUIDEX_CONNECT_URL;
16const appName = process.env.APP_NAME;
17
18const client = new SquidexClientManager(url, appName, clientId, clientSecret);
19
20function uniqueString(prefix) {
21 const dateStamp = new Date().toString();
22 return prefix ? `${prefix}-${dateStamp}` : dateStamp;
23}
24
25async function simpleWriteCheck(t, modelName, payload) {
26 const record = await client.CreateAsync(modelName, payload);
27 t.truthy(record.id);
28
29 const records = await client.RecordsAsync(modelName, { top: 0 });
30 t.true(records.items.length > 0);
31
32 await client.DeleteAsync(modelName, { id: record.id });
33}
34
35test.before('Get Models', async (t) => {
36 await client.ensureValidClient();
37 const models = client.Models();
38 t.true(Object.keys(models).length > 0);
39});
40
41test.serial('Articles', async (t) => {
42 const text = uniqueString('Testo');
43 const expected = { data: { title: { iv: text } }, publish: true };
44 const article = await client.CreateAsync('Articles', expected);
45 t.truthy(article);
46
47 // Make sure we can find the recently created article
48 const filter = await client.FilterRecordsAsync('Articles', expected, 'title');
49 const created = filter[0];
50 t.true(created !== undefined);
51 t.true(created.data.title.iv === expected.data.title.iv);
52
53 // Update exiting article and check the changes were made
54 const update = await client.UpdateAsync('Articles', {
55 id: article.id,
56 data: {
57 title: { iv: created.data.title.iv },
58 text: { iv: 'x' },
59 },
60 });
61 t.true(update.data.text.iv === 'x');
62 t.truthy(article.id);
63
64 // Create a image upload
65 const upload = await client.CreateAssetAsync(path.resolve(__dirname, '../GitHub/power-by.png'));
66
67 // Check update works via create or update call
68 const createOrUpdate = await client.CreateOrUpdateAsync('Articles', {
69 id: article.id,
70 data: {
71 title: { iv: update.data.title.iv },
72 text: { iv: 'y' },
73 image: { iv: [upload.body.id] },
74 },
75 }, 'title');
76 t.true(createOrUpdate.data.text.iv === 'y');
77
78 // Clean up
79 const deleteOp = await client.DeleteAsync('Articles', { id: article.id });
80 t.true(deleteOp.status === 204);
81});
82
83test.serial('Tag', async (t) => {
84 await simpleWriteCheck(t, 'Tag', {
85 publish: true,
86 data: {
87 name: { iv: uniqueString('cool') },
88 },
89 });
90});