Files
payloadcms/test/_community/int.spec.ts
2023-07-31 18:12:09 -04:00

73 lines
1.8 KiB
TypeScript

import payload from '../../src';
import { initPayloadTest } from '../helpers/configHelpers';
import { devUser } from '../credentials';
import { postsSlug } from './collections/Posts';
require('isomorphic-fetch');
let apiUrl;
let jwt;
const headers = {
'Content-Type': 'application/json',
};
const { email, password } = devUser;
describe('_Community Tests', () => {
// --__--__--__--__--__--__--__--__--__
// Boilerplate test setup/teardown
// --__--__--__--__--__--__--__--__--__
beforeAll(async () => {
const { serverURL } = await initPayloadTest({ __dirname, init: { local: false } });
apiUrl = `${serverURL}/api`;
const response = await fetch(`${apiUrl}/users/login`, {
body: JSON.stringify({
email,
password,
}),
headers,
method: 'post',
});
const data = await response.json();
jwt = data.token;
});
afterAll(async () => {
if (typeof payload.db.destroy === 'function') {
await payload.db.destroy(payload);
}
});
// --__--__--__--__--__--__--__--__--__
// You can run tests against the local API or the REST API
// use the tests below as a guide
// --__--__--__--__--__--__--__--__--__
it('local API example', async () => {
const newPost = await payload.create({
collection: postsSlug,
data: {
text: 'LOCAL API EXAMPLE',
},
});
expect(newPost.text).toEqual('LOCAL API EXAMPLE');
});
it('rest API example', async () => {
const newPost = await fetch(`${apiUrl}/${postsSlug}`, {
method: 'POST',
headers: {
...headers,
Authorization: `JWT ${jwt}`,
},
body: JSON.stringify({
text: 'REST API EXAMPLE',
}),
}).then((res) => res.json());
expect(newPost.doc.text).toEqual('REST API EXAMPLE');
});
});