test: adds dataloader test (#936)
* chore: adds dataloader test config * test: passing dataloader test
This commit is contained in:
47
test/dataloader/config.ts
Normal file
47
test/dataloader/config.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
import { buildConfig } from '../buildConfig';
|
||||||
|
import { devUser } from '../credentials';
|
||||||
|
|
||||||
|
export default buildConfig({
|
||||||
|
collections: [
|
||||||
|
{
|
||||||
|
slug: 'posts',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'text',
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'owner',
|
||||||
|
type: 'relationship',
|
||||||
|
relationTo: 'users',
|
||||||
|
hooks: {
|
||||||
|
beforeChange: [
|
||||||
|
({ req: { user } }) => user?.id,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
onInit: async (payload) => {
|
||||||
|
const user = await payload.create({
|
||||||
|
collection: 'users',
|
||||||
|
data: {
|
||||||
|
email: devUser.email,
|
||||||
|
password: devUser.password,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await payload.create({
|
||||||
|
user,
|
||||||
|
collection: 'posts',
|
||||||
|
data: postDoc,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const postDoc = {
|
||||||
|
title: 'test post',
|
||||||
|
};
|
||||||
53
test/dataloader/int.spec.ts
Normal file
53
test/dataloader/int.spec.ts
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import { GraphQLClient } from 'graphql-request';
|
||||||
|
import payload from '../../src';
|
||||||
|
import { devUser } from '../credentials';
|
||||||
|
import { initPayloadTest } from '../helpers/configHelpers';
|
||||||
|
import { postDoc } from './config';
|
||||||
|
|
||||||
|
describe('dataloader', () => {
|
||||||
|
let serverURL;
|
||||||
|
beforeAll(async () => {
|
||||||
|
const init = await initPayloadTest({ __dirname, init: { local: false } });
|
||||||
|
serverURL = init.serverURL;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('graphql', () => {
|
||||||
|
let client: GraphQLClient;
|
||||||
|
let token: string;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const url = `${serverURL}/api/graphql`;
|
||||||
|
client = new GraphQLClient(url);
|
||||||
|
|
||||||
|
const loginResult = await payload.login({
|
||||||
|
collection: 'users',
|
||||||
|
data: {
|
||||||
|
email: devUser.email,
|
||||||
|
password: devUser.password,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (loginResult.token) token = loginResult.token;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should allow querying via graphql', async () => {
|
||||||
|
const query = `query {
|
||||||
|
Posts {
|
||||||
|
docs {
|
||||||
|
title
|
||||||
|
owner {
|
||||||
|
email
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const response = await client.request(query, null, {
|
||||||
|
Authorization: `JWT ${token}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
const { docs } = response.Posts;
|
||||||
|
expect(docs[0].title).toStrictEqual(postDoc.title);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user