From dc52a1bb9c03e86de818af8b751b0ce1e624c9ce Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Mon, 29 Jun 2020 12:36:33 -0400 Subject: [PATCH] add rest collection tests for relationships --- .../requestHandlers/collections.spec.js | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/collections/requestHandlers/collections.spec.js b/src/collections/requestHandlers/collections.spec.js index a15b0e9a43..94227a73fd 100644 --- a/src/collections/requestHandlers/collections.spec.js +++ b/src/collections/requestHandlers/collections.spec.js @@ -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); + }); + }); });