add rest collection tests for relationships

This commit is contained in:
Dan Ribbens
2020-06-29 12:36:33 -04:00
parent 062cc08b14
commit dc52a1bb9c

View File

@@ -547,4 +547,76 @@ describe('Collections - REST', () => {
});
});
});
describe('Relationships', () => {
let documentA;
let documentB;
beforeAll(async (done) => {
// create document a
const createA = await fetch(`${url}/api/relationship-a`, {
body: JSON.stringify({}),
headers: {
Authorization: `JWT ${token}`,
'Content-Type': 'application/json',
},
method: 'post',
});
const createAData = await createA.json();
// create document b, related to a
const createB = await fetch(`${url}/api/relationship-b`, {
body: JSON.stringify({
post: [createAData.doc.id],
}),
headers: {
Authorization: `JWT ${token}`,
'Content-Type': 'application/json',
},
method: 'post',
});
// update a to relate to b
const createBData = await createB.json();
documentB = createBData.doc;
const updateA = await fetch(`${url}/api/relationship-a/${createAData.doc.id}`, {
body: JSON.stringify({
post: documentB.id,
}),
headers: {
Authorization: `JWT ${token}`,
'Content-Type': 'application/json',
},
method: 'put',
});
const updateAData = await updateA.json();
documentA = updateAData.doc;
done();
});
it('should create and read collections with relationships', async () => {
expect(documentA.post).not.toBeNull();
expect(documentB.post).toHaveLength(1);
});
it('should use depth to limit the number of relationships returned', async () => {
const response = await fetch(`${url}/api/relationship-a?depth=3`, {
headers: {
Authorization: `JWT ${token}`,
'Content-Type': 'application/json',
},
method: 'get',
});
const data = await response.json();
const [doc] = data.docs;
expect(doc.id).toBe(documentA.id);
let nested = doc.post;
expect(nested.id).toBe(documentB.id);
[nested] = nested.post;
expect(nested.id).toBe(documentA.id);
nested = nested.post;
expect(nested.id).toBe(documentB.id);
[nested] = nested.post;
expect(nested).not.toHaveProperty('post');
expect(nested).toBe(documentA.id);
});
});
});