chore: adds root endpoint test

This commit is contained in:
Jacob Fletcher
2022-09-22 13:23:42 -04:00
parent 52cd3b4a7e
commit 75bab716d1
4 changed files with 32 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { Response } from 'express';
import express, { Response } from 'express';
import { devUser } from '../credentials';
import { buildConfig } from '../buildConfig';
import { openAccess } from '../helpers/configHelpers';
@@ -91,9 +91,20 @@ const MyConfig: Config = {
method: 'get',
root: true,
handler: (req: PayloadRequest, res: Response): void => {
res.json({ message: 'Root.' });
res.json({ message: 'Hello, world!' });
},
},
{
path: `/${rootEndpoint}`,
method: 'post',
root: true,
handler: [
express.json({ type: 'application/json' }),
(req: PayloadRequest, res: Response): void => {
res.json(req.body);
}
],
},
],
onInit: async (payload) => {
await payload.create({

View File

@@ -1,6 +1,6 @@
import { initPayloadTest } from '../helpers/configHelpers';
import { RESTClient } from '../helpers/rest';
import { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug } from './config';
import { applicationEndpoint, collectionSlug, globalEndpoint, globalSlug, rootEndpoint } from './config';
require('isomorphic-fetch');
@@ -15,21 +15,21 @@ describe('Endpoints', () => {
describe('Collections', () => {
it('should GET a static endpoint', async () => {
const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/joe-bloggs`);
const { status, data } = await client.endpoint(`/api/${collectionSlug}/say-hello/joe-bloggs`);
expect(status).toBe(200);
expect(data.message).toStrictEqual('Hey Joey!');
});
it('should GET an endpoint with a parameter', async () => {
const name = 'George';
const { status, data } = await client.endpoint(`/${collectionSlug}/say-hello/${name}`);
const { status, data } = await client.endpoint(`/api/${collectionSlug}/say-hello/${name}`);
expect(status).toBe(200);
expect(data.message).toStrictEqual(`Hello ${name}!`);
});
it('should POST an endpoint with data', async () => {
const params = { name: 'George', age: 29 };
const { status, data } = await client.endpoint(`/${collectionSlug}/whoami`, 'post', params);
const { status, data } = await client.endpoint(`/api/${collectionSlug}/whoami`, 'post', params);
expect(status).toBe(200);
expect(data.name).toStrictEqual(params.name);
expect(data.age).toStrictEqual(params.age);
@@ -39,7 +39,7 @@ describe('Endpoints', () => {
describe('Globals', () => {
it('should call custom endpoint', async () => {
const params = { globals: 'response' };
const { status, data } = await client.endpoint(`/globals/${globalSlug}/${globalEndpoint}`, 'post', params);
const { status, data } = await client.endpoint(`/api/globals/${globalSlug}/${globalEndpoint}`, 'post', params);
expect(status).toBe(200);
expect(params).toMatchObject(data);
@@ -49,7 +49,17 @@ describe('Endpoints', () => {
describe('API', () => {
it('should call custom endpoint', async () => {
const params = { app: 'response' };
const { status, data } = await client.endpoint(`/${applicationEndpoint}`, 'post', params);
const { status, data } = await client.endpoint(`/api/${applicationEndpoint}`, 'post', params);
expect(status).toBe(200);
expect(params).toMatchObject(data);
});
});
describe('Root', () => {
it('should call custom root endpoint', async () => {
const params = { root: 'response' };
const { status, data } = await client.endpoint(`/${rootEndpoint}`, 'post', params);
expect(status).toBe(200);
expect(params).toMatchObject(data);

View File

@@ -255,8 +255,8 @@ export class RESTClient {
return { status, doc: result };
}
async endpoint<T = any>(path: string, method = 'get', params = undefined): Promise<{status: number, data: T}> {
const response = await fetch(`${this.serverURL}/api${path}`, {
async endpoint<T = any>(path: string, method = 'get', params = undefined): Promise<{ status: number, data: T }> {
const response = await fetch(`${this.serverURL}${path}`, {
headers: {
'Content-Type': 'application/json',
},