Files
directus/packages/sdk/tests/base/directus.test.ts
José Varela 23f6baa4db SDK: refactor constructors (#9080)
* refactor: more intuitive interfaces

* refactor: simpler refresh
before: on every request we were debouncing a refresh request
after: call refresh only once before now() + 'expires'

* refactor: prefix on base storage

* fixup! refactor: simpler refresh before: on every request we were debouncing a refresh request after: call refresh only once before now() + 'expires'

* refactor: simpler axios transport
before: handle auth headers
after: auth headers are handled on directus instance

* refactor: simpler usage of Directus constructor

* fixup! refactor: simpler refresh before: on every request we were debouncing a refresh request after: call refresh only once before now() + 'expires'

* refactor: fix tests based on previous changes

* refactor: better auth constructor
before: depends on SDK instance
after: depends on Transport and Storage instance

* accept staticToken from auth

* make transport and storage as optional on options

* fix type auth refresh

* simplify transport

* fix test for previous changes

* improve auth class

* revert some IAuth props because tests

* allow to force memory of localstorage on storage

* add tests for previous change

* document everything and simplify some things

* fix override headers on request

* better name typing

* fix private axios

* removed boolean from CLI auth.refresh()

* fix missing url in some examples

* soem grammar updates

Co-authored-by: Jay Cammarano <jay.cammarano@gmail.com>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
2021-11-04 13:30:30 -04:00

179 lines
4.0 KiB
TypeScript

/**
* @jest-environment node
*/
import { Auth } from '../../src/base/auth';
import { ItemsHandler } from '../../src/base/items';
import { Transport } from '../../src/base/transport';
import { Directus } from '../../src/base';
import {
ActivityHandler,
CollectionsHandler,
CommentsHandler,
FieldsHandler,
FilesHandler,
FoldersHandler,
PermissionsHandler,
PresetsHandler,
RelationsHandler,
RevisionsHandler,
RolesHandler,
ServerHandler,
SettingsHandler,
UsersHandler,
UtilsHandler,
} from '../../src/handlers';
import { test } from '../utils';
import { InvitesHandler } from '../../src/handlers/invites';
import { TFAHandler } from '../../src/handlers/tfa';
import { MeHandler } from '../../src/handlers/me';
describe('sdk', function () {
const sdk = new Directus('http://example.com');
it('has auth', function () {
expect(sdk.auth).toBeInstanceOf(Auth);
});
it('has transport', function () {
expect(sdk.transport).toBeInstanceOf(Transport);
});
it('has activity instance', function () {
expect(sdk.activity).toBeInstanceOf(ActivityHandler);
});
it('has activity instance', function () {
expect(sdk.activity.comments).toBeInstanceOf(CommentsHandler);
});
it('has collections instance', function () {
expect(sdk.collections).toBeInstanceOf(CollectionsHandler);
});
it('has fields instance', function () {
expect(sdk.fields).toBeInstanceOf(FieldsHandler);
});
it('has files instance', function () {
expect(sdk.files).toBeInstanceOf(FilesHandler);
});
it('has folders instance', function () {
expect(sdk.folders).toBeInstanceOf(FoldersHandler);
});
it('has permissions instance', function () {
expect(sdk.permissions).toBeInstanceOf(PermissionsHandler);
});
it('has presets instance', function () {
expect(sdk.presets).toBeInstanceOf(PresetsHandler);
});
it('has relations instance', function () {
expect(sdk.relations).toBeInstanceOf(RelationsHandler);
});
it('has revisions instance', function () {
expect(sdk.revisions).toBeInstanceOf(RevisionsHandler);
});
it('has roles instance', function () {
expect(sdk.roles).toBeInstanceOf(RolesHandler);
});
it('has server instance', function () {
expect(sdk.server).toBeInstanceOf(ServerHandler);
});
it('has settings instance', function () {
expect(sdk.settings).toBeInstanceOf(SettingsHandler);
});
it('has users instance', function () {
expect(sdk.users).toBeInstanceOf(UsersHandler);
});
it('has users invites', function () {
expect(sdk.users.invites).toBeInstanceOf(InvitesHandler);
});
it('has user profile', function () {
expect(sdk.users.me).toBeInstanceOf(MeHandler);
});
it('has users tfa', function () {
expect(sdk.users.me.tfa).toBeInstanceOf(TFAHandler);
});
it('has utils instance', function () {
expect(sdk.utils).toBeInstanceOf(UtilsHandler);
});
it('has items', async function () {
expect(sdk.items('collection')).toBeInstanceOf(ItemsHandler);
});
test('can run graphql', async function (url, nock) {
const scope = nock()
.post('/graphql')
.reply(200, {
data: {
posts: [
{ id: 1, title: 'My first post' },
{ id: 2, title: 'My second post' },
],
},
});
const query = `
query {
posts {
id
title
}
}
`;
const sdk = new Directus(url);
const response = await sdk.graphql.items(query);
expect(response.data).toMatchObject({
posts: [
{ id: 1, title: 'My first post' },
{ id: 2, title: 'My second post' },
],
});
expect(scope.pendingMocks().length).toBe(0);
});
test('can run graphql on system', async function (url, nock) {
const scope = nock()
.post('/graphql/system')
.reply(200, {
data: {
users: [{ email: 'someone@example.com' }, { email: 'someone.else@example.com' }],
},
});
const query = `
query {
users {
email
}
}
`;
const sdk = new Directus(url);
const response = await sdk.graphql.system(query);
expect(response.data).toMatchObject({
users: [{ email: 'someone@example.com' }, { email: 'someone.else@example.com' }],
});
expect(scope.pendingMocks().length).toBe(0);
});
});