diff --git a/jest.config.js b/jest.config.js index 863003b..cec63b6 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,13 +1,12 @@ module.exports = { - preset: 'ts-jest', - setupTestFrameworkScriptFile: './jest.setup.js', - testEnvironment: 'node', clearMocks : true, moduleFileExtensions: [ "ts", "js", ], + preset: 'ts-jest', + testEnvironment: 'node', testMatch: [ "**/*.test.ts" ], -}; \ No newline at end of file +}; diff --git a/package-lock.json b/package-lock.json index d2582e1..46b2ce6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5090,7 +5090,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -5111,12 +5112,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -5131,17 +5134,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -5258,7 +5264,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -5270,6 +5277,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -5284,6 +5292,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -5291,12 +5300,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -5315,6 +5326,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -5395,7 +5407,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -5407,6 +5420,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -5492,7 +5506,8 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -5528,6 +5543,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -5547,6 +5563,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -5590,12 +5607,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/src/schema/common/common-entity-interface.ts b/src/schema/common/common-entity-interface.ts index af82292..c0ac6b2 100644 --- a/src/schema/common/common-entity-interface.ts +++ b/src/schema/common/common-entity-interface.ts @@ -2,5 +2,6 @@ export const CommonEntityInterface = `interface CommonEntity { id: ID! creationDate: Date, lastUpdateDate: Date, - dataVersion: Int! + dataVersion: Int!, + realityId: Int! }`; diff --git a/test/integration-tests/test-server/client.ts b/test/integration-tests/test-server/client.ts index 393149f..9230aed 100644 --- a/test/integration-tests/test-server/client.ts +++ b/test/integration-tests/test-server/client.ts @@ -5,7 +5,7 @@ export const url = `http://localhost:${polarisPropertiesPath.port}${ polarisPropertiesPath.endpoint }`; -export const graphqlRequest = async (data: string, headers: any, variables: any) => { +export const graphQLRequest = async (data: string, headers: any, variables: any = undefined) => { const graphQLClient = new GraphQLClient(url, { headers }); return graphQLClient.request(data, variables); }; diff --git a/test/integration-tests/test-server/dal/author-model.ts b/test/integration-tests/test-server/dal/author-model.ts new file mode 100644 index 0000000..01bd7fd --- /dev/null +++ b/test/integration-tests/test-server/dal/author-model.ts @@ -0,0 +1,16 @@ +import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver'; +import { Schema } from 'mongoose'; + +export interface Author extends RepositoryModel { + testId: string; + firstName: string; + lastName: string; +} + +export const authorSchema: Schema = new Schema({ + testId: String, + firstName: String, + lastName: String, +}); + +export const AuthorModelPerReality = getModelCreator('author', authorSchema); diff --git a/test/integration-tests/test-server/dal/book-model.ts b/test/integration-tests/test-server/dal/book-model.ts index 46fc73d..1d565db 100644 --- a/test/integration-tests/test-server/dal/book-model.ts +++ b/test/integration-tests/test-server/dal/book-model.ts @@ -1,20 +1,24 @@ -import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver'; +import { getModelCreator, RepositoryModel, SchemaCreator } from '@enigmatis/mongo-driver'; import { Schema } from 'mongoose'; +import { Author } from './author-model'; export interface Book extends RepositoryModel { testId: string; title: string; - author: string; - otherBook: Book; + author: Author; dataVersion: number; } -export const bookSchema: Schema = new Schema({ - testId: String, - title: String, - author: String, - otherBook: Object, - dataVersion: Number, -}); +export const bookSchema: SchemaCreator = refNameCreator => { + return new Schema({ + testId: String, + title: String, + author: { + type: Schema.Types.ObjectId, + ref: refNameCreator('author'), + }, + dataVersion: Number, + }); +}; export const BookModelPerReality = getModelCreator('book', bookSchema); diff --git a/test/integration-tests/test-server/run-test.ts b/test/integration-tests/test-server/run-test.ts index 311a7af..5c6c33d 100644 --- a/test/integration-tests/test-server/run-test.ts +++ b/test/integration-tests/test-server/run-test.ts @@ -2,7 +2,8 @@ import { closeConnection, initConnection } from '@enigmatis/mongo-driver'; import { config } from 'dotenv'; import * as mongoose from 'mongoose'; import { logger, server } from './server'; -export async function init() { + +export async function startTestServer() { config(); jest.setTimeout(15000); const connectionString = @@ -12,7 +13,7 @@ export async function init() { server.start(); } -export async function finish() { +export async function stopTestServer() { await closeConnection(); await server.stop(); } diff --git a/test/integration-tests/test-server/schema/definitions/author.ts b/test/integration-tests/test-server/schema/definitions/author.ts new file mode 100644 index 0000000..98bc684 --- /dev/null +++ b/test/integration-tests/test-server/schema/definitions/author.ts @@ -0,0 +1,8 @@ +import { RepositoryEntity } from '../../../../../src/main'; + +export interface Author extends RepositoryEntity { + testId: string; + id: string; + firstName: string; + lastName: string; +} diff --git a/test/integration-tests/test-server/schema/definitions/book.ts b/test/integration-tests/test-server/schema/definitions/book.ts index 87a9a19..8b67420 100644 --- a/test/integration-tests/test-server/schema/definitions/book.ts +++ b/test/integration-tests/test-server/schema/definitions/book.ts @@ -1,9 +1,9 @@ import { RepositoryEntity } from '../../../../../src/main'; +import { Author } from './author'; export interface Book extends RepositoryEntity { testId: string; id: string; title: string; - author: string; - otherBook?: Book; + author: Author; } diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts index 6cf151a..93e2c41 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts @@ -1,6 +1,8 @@ import { QueryWithIrrelevant } from '@enigmatis/mongo-driver'; +import { PolarisRequestHeaders } from '@enigmatis/utills'; import { UserInputError } from 'apollo-server-koa'; import { PolarisContext } from '../../../../../src/server/polaris-context'; +import { AuthorModelPerReality } from '../../dal/author-model'; import { BookModelPerReality } from '../../dal/book-model'; import { Book } from '../definitions/book'; import { BOOK_UPDATED } from './subscription-event-names'; @@ -34,9 +36,18 @@ export const bookResolver = async ( query: object, context: PolarisContext, ) => { - const { realityId } = context.headers; + const { realityId, includeLinkedOperation } = context.headers; if (!Number.isInteger(realityId as any)) { throw new UserInputError('please provide reality-id header'); + } else if (includeLinkedOperation) { + const zeroRealityHeaders = { ...context.headers }; + zeroRealityHeaders.realityId = 0; + const zeroRealityContext = { ...context }; + zeroRealityContext.headers = zeroRealityHeaders; + return BookModelPerReality(context) + .find({}) + .populate({ path: 'author', model: AuthorModelPerReality(zeroRealityContext) }) + .lean(); } else { return BookModelPerReality(context) .find({}) diff --git a/test/integration-tests/test-server/schema/schema.ts b/test/integration-tests/test-server/schema/schema.ts index a5d3593..6078d5a 100644 --- a/test/integration-tests/test-server/schema/schema.ts +++ b/test/integration-tests/test-server/schema/schema.ts @@ -1,10 +1,18 @@ import { GraphQLSchema } from 'graphql'; import { makeExecutablePolarisSchema } from '../../../../src/main'; import { resolvers } from './resolvers/book-resolvers'; -import { Book, BookInput, Mutation, Query, Subscription } from './types/schema-types'; +import { + Author, + AuthorInput, + Book, + BookInput, + Mutation, + Query, + Subscription, +} from './types/schema-types'; export const schema: GraphQLSchema = makeExecutablePolarisSchema({ - typeDefs: [Book, BookInput, Mutation, Query, Subscription], + typeDefs: [Author, AuthorInput, Book, BookInput, Mutation, Query, Subscription], resolvers: [resolvers], resolverValidationOptions: { requireResolversForResolveType: false, diff --git a/test/integration-tests/test-server/schema/types/input/author-input.ts b/test/integration-tests/test-server/schema/types/input/author-input.ts new file mode 100644 index 0000000..c8341b6 --- /dev/null +++ b/test/integration-tests/test-server/schema/types/input/author-input.ts @@ -0,0 +1,10 @@ +export const AuthorInput = `input AuthorInput { + id: ID!, + testId: String!, + firstName: String, + lastName: String + }, + input UpdateAuthorInput { + firstName: String, + lastName: String + }`; diff --git a/test/integration-tests/test-server/schema/types/input/book-input.ts b/test/integration-tests/test-server/schema/types/input/book-input.ts index 6c8de88..c5cd771 100644 --- a/test/integration-tests/test-server/schema/types/input/book-input.ts +++ b/test/integration-tests/test-server/schema/types/input/book-input.ts @@ -2,11 +2,9 @@ export const BookInput = `input BookInput { id: ID, testId: String!, title: String, - author: String, - otherBook: BookInput + author: AuthorInput, }, input UpdateBookInput { title: String, - author: String, - otherBook: BookInput + author: AuthorInput, }`; diff --git a/test/integration-tests/test-server/schema/types/output/author.ts b/test/integration-tests/test-server/schema/types/output/author.ts new file mode 100644 index 0000000..cc5744b --- /dev/null +++ b/test/integration-tests/test-server/schema/types/output/author.ts @@ -0,0 +1,9 @@ +export const Author = `type Author implements CommonEntity { + id: ID! + firstName: String, + lastName: String, + creationDate: Date, + lastUpdateDate: Date, + dataVersion: Int!, + realityId: Int! + }`; diff --git a/test/integration-tests/test-server/schema/types/output/book.ts b/test/integration-tests/test-server/schema/types/output/book.ts index 9b7abc5..30d99cd 100644 --- a/test/integration-tests/test-server/schema/types/output/book.ts +++ b/test/integration-tests/test-server/schema/types/output/book.ts @@ -1,11 +1,10 @@ export const Book = `type Book implements CommonEntity { testId: String, id: ID!, + title: String, + author: Author, creationDate: Date, lastUpdateDate: Date, dataVersion: Int!, - title: String, - author: String, - otherBook: ID, realityId: Int! }`; diff --git a/test/integration-tests/test-server/schema/types/schema-types.ts b/test/integration-tests/test-server/schema/types/schema-types.ts index b5a6f6e..2d9c685 100644 --- a/test/integration-tests/test-server/schema/types/schema-types.ts +++ b/test/integration-tests/test-server/schema/types/schema-types.ts @@ -3,3 +3,5 @@ export { Mutation } from './mutation/root-mutation'; export { Book } from './output/book'; export { Query } from './query/root-query'; export { Subscription } from './subscription/root-subscription'; +export { Author } from './output/author'; +export { AuthorInput } from './input/author-input'; diff --git a/test/integration-tests/tests/irrelevant-entities.test.ts b/test/integration-tests/tests/irrelevant-entities.test.ts index 059058c..4c1b9cc 100644 --- a/test/integration-tests/tests/irrelevant-entities.test.ts +++ b/test/integration-tests/tests/irrelevant-entities.test.ts @@ -1,13 +1,13 @@ import { PolarisRequestHeaders } from '@enigmatis/utills'; -import { graphqlRawRequest, graphqlRequest } from '../test-server/client'; +import { graphqlRawRequest, graphQLRequest } from '../test-server/client'; import { BookModelPerReality } from '../test-server/dal/book-model'; -import { finish, init } from '../test-server/run-test'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; export const titles = ['first', 'second', 'third', 'fourth', 'fifth']; const prepareDb = async (headers: PolarisRequestHeaders) => { const books = []; for (let i = 0; i < titles.length; i++) { - books.push({ author: 'arik', title: titles[i], testId: i, dataVersion: i + 1 }); + books.push({ title: titles[i], testId: i, dataVersion: i + 1 }); } await BookModelPerReality({ headers }).create(books); }; @@ -15,12 +15,12 @@ const prepareDb = async (headers: PolarisRequestHeaders) => { const requestHeaders = { 'reality-id': 1, 'data-version': 1 }; beforeEach(async () => { - await init(); + await startTestServer(); await prepareDb({ realityId: 1 }); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('irrelevant entities tests', () => { @@ -89,14 +89,14 @@ describe('irrelevant entities tests', () => { const deleteBookMutation = `mutation deleteBook ($bookId:String!) { deleteBook(bookId: $bookId){ title }}`; - const result: any = await graphqlRequest(queryBookStartsWith, requestHeaders, {}); + const result: any = await graphQLRequest(queryBookStartsWith, requestHeaders); const idToDelete = result.booksStartsWith[0].testId; const expectedIrrelevantId = result.booksStartsWith[0].id; - await graphqlRequest(deleteBookMutation, { 'reality-id': 1 }, { bookId: idToDelete }); + await graphQLRequest(deleteBookMutation, { 'reality-id': 1 }, { bookId: idToDelete }); const { extensions }: any = await graphqlRawRequest(queryBookStartsWith, requestHeaders); expect( extensions.irrelevantEntities.booksStartsWith.includes(expectedIrrelevantId), ).toBeTruthy(); - }, 300000); + }); }); diff --git a/test/integration-tests/tests/mutation.test.ts b/test/integration-tests/tests/mutation.test.ts index 339b1aa..4e32846 100644 --- a/test/integration-tests/tests/mutation.test.ts +++ b/test/integration-tests/tests/mutation.test.ts @@ -1,5 +1,5 @@ -import { graphqlRequest } from '../test-server/client'; -import { finish, init } from '../test-server/run-test'; +import { graphQLRequest } from '../test-server/client'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; const headers = { 'reality-id': 1 }; const createBookMutation = `mutation createBook ($book:BookInput!) {createBook(book:$book){testId}}`; @@ -9,49 +9,49 @@ const updateBookMutation = `mutation updateBook ($bookId:String!, $update:Update const findBookQuery = `query bookById ($bookId:String!) {bookById(bookId:$bookId){title}}`; const deleteBookMutation = `mutation deleteBook ($bookId:String!) { deleteBook(bookId: $bookId){ title }}`; -const defaultBookVariables = (title: string, testId: string) => ({ author: 'chen', title, testId }); +const defaultBookVariables = (title: string, testId: string) => ({ title, testId }); beforeEach(() => { - return init(); + return startTestServer(); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('mutation tests', () => { test('create book, book is created ', async () => { const id = '1'; const title = 'book'; - await graphqlRequest(createBookMutation, headers, { + await graphQLRequest(createBookMutation, headers, { book: defaultBookVariables(title, id), }); - const response: any = await graphqlRequest(findBookQuery, headers, { bookId: id }); + const response: any = await graphQLRequest(findBookQuery, headers, { bookId: id }); expect(response.bookById.title).toEqual(title); - await graphqlRequest(deleteBookMutation, headers, { bookId: id }); + await graphQLRequest(deleteBookMutation, headers, { bookId: id }); }); test('update book, book is updated ', async () => { const id = '1'; - await graphqlRequest(createBookMutation, headers, { + await graphQLRequest(createBookMutation, headers, { book: defaultBookVariables('book', id), }); const updatedTitle = 'book updated'; - await graphqlRequest(updateBookMutation, headers, { + await graphQLRequest(updateBookMutation, headers, { bookId: id, update: { title: updatedTitle }, }); - const response: any = await graphqlRequest(findBookQuery, headers, { bookId: id }); + const response: any = await graphQLRequest(findBookQuery, headers, { bookId: id }); expect(response.bookById.title).toEqual(updatedTitle); - await graphqlRequest(deleteBookMutation, headers, { bookId: id }); + await graphQLRequest(deleteBookMutation, headers, { bookId: id }); }); test('delete book, book is deleted ', async () => { const id = '1'; - await graphqlRequest(createBookMutation, headers, { + await graphQLRequest(createBookMutation, headers, { book: defaultBookVariables('book', id), }); - await graphqlRequest(deleteBookMutation, headers, { bookId: id }); - const response: any = await graphqlRequest(findBookQuery, headers, { bookId: id }); + await graphQLRequest(deleteBookMutation, headers, { bookId: id }); + const response: any = await graphQLRequest(findBookQuery, headers, { bookId: id }); expect(response.bookById).toBeNull(); }); }); diff --git a/test/integration-tests/tests/query.test.ts b/test/integration-tests/tests/query.test.ts index 3d72dec..06ed7ef 100644 --- a/test/integration-tests/tests/query.test.ts +++ b/test/integration-tests/tests/query.test.ts @@ -1,14 +1,14 @@ import { PolarisRequestHeaders } from '@enigmatis/utills'; -import { graphqlRequest } from '../test-server/client'; +import { graphQLRequest } from '../test-server/client'; import { BookModelPerReality } from '../test-server/dal/book-model'; -import { finish, init } from '../test-server/run-test'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; const titles = ['first', 'second', 'third', 'fourth', 'fifth']; const prepareDb = async (headers: PolarisRequestHeaders) => { const books = []; for (let i = 0; i < titles.length; i++) { - books.push({ author: 'chen', title: titles[i], testId: i }); + books.push({ title: titles[i], testId: i }); } await BookModelPerReality({ headers }).create(books); }; @@ -16,19 +16,19 @@ const prepareDb = async (headers: PolarisRequestHeaders) => { const requestHeaders = { 'reality-id': 1 }; beforeEach(async () => { - await init(); + await startTestServer(); await prepareDb({ realityId: 1 }); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('query tests', () => { test('fetch single book', async () => { const queryBook = `query bookById ($bookId:String!) {bookById(bookId:$bookId){title}}`; for (let i = 0; i < titles.length; i++) { - const response: any = await graphqlRequest(queryBook, requestHeaders, { + const response: any = await graphQLRequest(queryBook, requestHeaders, { bookId: i.toString(), }); expect(response.bookById.title).toEqual(titles[i]); @@ -36,7 +36,7 @@ describe('query tests', () => { }); test('fetch all books', async () => { const queryBook = `query{books{title}}`; - const response: any = await graphqlRequest(queryBook, requestHeaders, {}); + const response: any = await graphQLRequest(queryBook, requestHeaders, {}); const responseTitles = []; for (const book of response.books) { responseTitles.push(book.title); diff --git a/test/integration-tests/tests/reality.test.ts b/test/integration-tests/tests/reality.test.ts new file mode 100644 index 0000000..dc2f19c --- /dev/null +++ b/test/integration-tests/tests/reality.test.ts @@ -0,0 +1,123 @@ +import { graphQLRequest } from '../test-server/client'; +import { AuthorModelPerReality } from '../test-server/dal/author-model'; +import { BookModelPerReality } from '../test-server/dal/book-model'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; + +const dbRealityIdHeader = (realityId: any) => ({ realityId }); +const requestRealityIdHeader = (realityId: any) => ({ 'reality-id': realityId }); +const realityIdHeaderWithIncludeLinkedOper = (realityId: number) => ({ + 'reality-id': realityId, + 'include-linked-oper': true, +}); + +export const titleArray: string[] = ['first', 'second', 'third', 'fourth', 'fifth']; + +const prepareDb = async () => { + const firstAuthor = await AuthorModelPerReality({ headers: dbRealityIdHeader(0) }).create({ + testId: 0, + firstName: 'Foo', + lastName: 'Bar', + }); + + const secondAuthor = await AuthorModelPerReality({ headers: dbRealityIdHeader(2) }).create({ + testId: 0, + firstName: 'Hello', + lastName: 'World', + }); + + const books = []; + for (let i = 0; i < titleArray.length; i++) { + books.push({ testId: i, title: titleArray[i], dataVersion: i + 1 }); + } + await BookModelPerReality({ headers: dbRealityIdHeader(1) }).create(books); + + await BookModelPerReality({ headers: dbRealityIdHeader(3) }).create({ + testId: 0, + title: 'Shadow Realm', + author: firstAuthor, + }); + + await BookModelPerReality({ headers: dbRealityIdHeader(4) }).create({ + testId: 0, + title: 'Lorem Ipsum', + author: secondAuthor, + }); +}; + +beforeEach(async () => { + await startTestServer(); + await prepareDb(); +}); + +afterEach(() => { + return stopTestServer(); +}); + +describe('reality tests', () => { + test('fetch entities from specific reality', async () => { + const queryBook = `query{books{realityId}}`; + const realityId: number = 1; + const response: any = await graphQLRequest(queryBook, requestRealityIdHeader(realityId)); + const responseRealities = []; + for (const book of response.books) { + responseRealities.push(book.realityId); + } + const uniqueResponseRealities = [...new Set(responseRealities)]; + expect(uniqueResponseRealities.length).toBe(1); + expect(uniqueResponseRealities).toContain(realityId); + }); + + test('fetch entities from a non existing reality', async () => { + const emptyResult: [] = []; + const queryBook = `query{books{realityId}}`; + const realityId: number = 999; + const response: any = await graphQLRequest(queryBook, requestRealityIdHeader(realityId)); + expect(response.books).toEqual(emptyResult); + }); + + test('fetch entities without reality header', async () => { + const queryBook = `query{books{title}}`; + await expect(graphQLRequest(queryBook, undefined)).rejects.toThrow( + 'please provide reality-id header', + ); + }); + + test('fetch entities with invalid reality header', async () => { + const queryBook = `query{books{title}}`; + await expect(graphQLRequest(queryBook, requestRealityIdHeader('oops'))).rejects.toThrow( + 'Unable to format headers', + ); + }); + + test('fetch sub entities from specific reality with linked oper entities', async () => { + const queryBook = `query{books{realityId author{realityId}}}`; + const realityId: number = 3; + const response: any = await graphQLRequest( + queryBook, + realityIdHeaderWithIncludeLinkedOper(realityId), + ); + const responseRealities = []; + const responseSubRealities = []; + for (const book of response.books) { + responseRealities.push(book.realityId); + responseSubRealities.push(book.author.realityId); + } + const uniqueResponseRealities = [...new Set(responseRealities)]; + const uniqueResponseSubRealities = [...new Set(responseSubRealities)]; + expect(uniqueResponseRealities.length).toBe(1); + expect(uniqueResponseRealities).toContain(realityId); + expect(uniqueResponseSubRealities).toContain(0); + }); + + test('fetch sub entities from specific reality without linked oper entities', async () => { + const queryBook = `query{books{realityId title author{realityId}}}`; + const realityId: number = 4; + const response: any = await graphQLRequest( + queryBook, + realityIdHeaderWithIncludeLinkedOper(realityId), + ); + for (const book of response.books) { + expect(book.author).toBeNull(); + } + }); +});