From be7364966b0e7e91a9e94afbfe37892dbbb61f46 Mon Sep 17 00:00:00 2001 From: Sayed Ali Sharifian Date: Wed, 23 Nov 2022 22:10:13 +0330 Subject: [PATCH 1/5] test: add coverage for `IsDocument` --- tests/document.test.ts | 102 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 tests/document.test.ts diff --git a/tests/document.test.ts b/tests/document.test.ts new file mode 100644 index 0000000..4be1d5e --- /dev/null +++ b/tests/document.test.ts @@ -0,0 +1,102 @@ +import { Store } from '../src' +import { isDocument } from '../src/document' +import { User, schema, TypesMap } from './fixtures' + +const store = new Store({ + schema, +}) + +afterEach(() => store.reset()) + +it('should return `true` if the input is document', () => { + const data: User = { + id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', + username: 'john.doe', + email: 'john.doe@example.com', + profile: { + username: 'john.doe', + bio: 'Excepturz.', + avatar: { + url: 'https://example.com/avatar.png', + height: 680, + width: 970, + size: '226kb', + }, + followers: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + following: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + posts: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + joinedAt: '2022-11-08T15:34:44.339Z', + }, + } + + store.add('User', data) + const user = store.findFirst('User') + + expect(isDocument(user)).toEqual(true) +}) + +it('should return `false` if the input is not a document', () => { + const data: User = { + id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', + username: 'john.doe', + email: 'john.doe@example.com', + profile: { + username: 'john.doe', + bio: 'Excepturz.', + avatar: { + url: 'https://example.com/avatar.png', + height: 680, + width: 970, + size: '226kb', + }, + followers: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + following: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + posts: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + joinedAt: '2022-11-08T15:34:44.339Z', + }, + } + + expect(isDocument(data)).toEqual(false) +}) From 266d494c84e568761dbbc8061f55f0f935cc5922 Mon Sep 17 00:00:00 2001 From: Sayed Ali Sharifian Date: Thu, 24 Nov 2022 22:39:49 +0330 Subject: [PATCH 2/5] test: add test for `getDocumentType()` --- tests/document.test.ts | 206 ++++++++++++++++++++++++++--------------- 1 file changed, 129 insertions(+), 77 deletions(-) diff --git a/tests/document.test.ts b/tests/document.test.ts index 4be1d5e..5e6deaa 100644 --- a/tests/document.test.ts +++ b/tests/document.test.ts @@ -1,5 +1,6 @@ import { Store } from '../src' -import { isDocument } from '../src/document' +import { getDocumentType, isDocument } from '../src/document' +import { Document } from '../src/types' import { User, schema, TypesMap } from './fixtures' const store = new Store({ @@ -8,95 +9,146 @@ const store = new Store({ afterEach(() => store.reset()) -it('should return `true` if the input is document', () => { - const data: User = { - id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', - username: 'john.doe', - email: 'john.doe@example.com', - profile: { +describe('`isDocument`', () => { + it('should return `true` if the input is document', () => { + const data: User = { + id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', username: 'john.doe', - bio: 'Excepturz.', - avatar: { - url: 'https://example.com/avatar.png', - height: 680, - width: 970, - size: '226kb', - }, - followers: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, + email: 'john.doe@example.com', + profile: { + username: 'john.doe', + bio: 'Excepturz.', + avatar: { + url: 'https://example.com/avatar.png', + height: 680, + width: 970, + size: '226kb', }, - }, - following: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, + followers: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, }, - }, - posts: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, + following: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + posts: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, }, + joinedAt: '2022-11-08T15:34:44.339Z', }, - joinedAt: '2022-11-08T15:34:44.339Z', - }, - } + } - store.add('User', data) - const user = store.findFirst('User') + store.add('User', data) + const user = store.findFirst('User') - expect(isDocument(user)).toEqual(true) -}) + expect(isDocument(user)).toEqual(true) + }) -it('should return `false` if the input is not a document', () => { - const data: User = { - id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', - username: 'john.doe', - email: 'john.doe@example.com', - profile: { + it('should return `false` if the input is not a document', () => { + const data: User = { + id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', username: 'john.doe', - bio: 'Excepturz.', - avatar: { - url: 'https://example.com/avatar.png', - height: 680, - width: 970, - size: '226kb', - }, - followers: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, + email: 'john.doe@example.com', + profile: { + username: 'john.doe', + bio: 'Excepturz.', + avatar: { + url: 'https://example.com/avatar.png', + height: 680, + width: 970, + size: '226kb', }, - }, - following: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, + followers: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + following: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, }, + posts: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + joinedAt: '2022-11-08T15:34:44.339Z', }, - posts: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, + } + + expect(isDocument(data)).toEqual(false) + }) +}) +describe('`getDocumentType`', () => { + it('should get the document type', () => { + const data: User = { + id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', + username: 'john.doe', + email: 'john.doe@example.com', + profile: { + username: 'john.doe', + bio: 'Excepturz.', + avatar: { + url: 'https://example.com/avatar.png', + height: 680, + width: 970, + size: '226kb', }, + followers: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + following: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + posts: { + count: 0, + edges: [], + pageInfo: { + hasNextPage: false, + hasPreviousPage: false, + }, + }, + joinedAt: '2022-11-08T15:34:44.339Z', }, - joinedAt: '2022-11-08T15:34:44.339Z', - }, - } + } + + store.add('User', data) + const user = store.findFirstOrThrow('User') as Document - expect(isDocument(data)).toEqual(false) + expect(getDocumentType(user)).toEqual('User') + }) }) From 4665641f48ff31c36336a18ed460d950f4145353 Mon Sep 17 00:00:00 2001 From: Sayed Ali Sharifian Date: Tue, 29 Nov 2022 23:39:46 +0330 Subject: [PATCH 3/5] refactor: move the test files --- tests/{ => units}/document.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename tests/{ => units}/document.test.ts (94%) diff --git a/tests/document.test.ts b/tests/units/document.test.ts similarity index 94% rename from tests/document.test.ts rename to tests/units/document.test.ts index 5e6deaa..9473d1d 100644 --- a/tests/document.test.ts +++ b/tests/units/document.test.ts @@ -1,7 +1,7 @@ -import { Store } from '../src' -import { getDocumentType, isDocument } from '../src/document' -import { Document } from '../src/types' -import { User, schema, TypesMap } from './fixtures' +import { Store } from '../../src' +import { getDocumentType, isDocument } from '../../src/document' +import { Document } from '../../src/types' +import { User, schema, TypesMap } from '../fixtures' const store = new Store({ schema, From 4ebb96214244e15bc2f10882a56f37065446813e Mon Sep 17 00:00:00 2001 From: Sayed Ali Sharifian Date: Sun, 4 Dec 2022 19:35:05 +0330 Subject: [PATCH 4/5] refactor: change the data generation --- tests/unit/document.test.ts | 36 ++++++++ tests/units/document.test.ts | 154 ----------------------------------- 2 files changed, 36 insertions(+), 154 deletions(-) create mode 100644 tests/unit/document.test.ts delete mode 100644 tests/units/document.test.ts diff --git a/tests/unit/document.test.ts b/tests/unit/document.test.ts new file mode 100644 index 0000000..0e45bd1 --- /dev/null +++ b/tests/unit/document.test.ts @@ -0,0 +1,36 @@ +import { Store } from '../../src' +import { getDocumentType, isDocument } from '../../src/document' +import { Document } from '../../src/types' +import { User, schema, TypesMap } from '../fixtures' +import { userFactory } from '../utils/factories' + +const store = new Store({ + schema, +}) + +afterEach(() => store.reset()) + +describe('`isDocument`', () => { + it('should return `true` if the input is document', () => { + const data = userFactory() + store.add('User', data) + const user = store.findFirst('User') + + expect(isDocument(user)).toEqual(true) + }) + + it('should return `false` if the input is not a document', () => { + const data = userFactory() + expect(isDocument(data)).toEqual(false) + }) +}) +describe('`getDocumentType`', () => { + it('should get the document type', () => { + const data = userFactory() + + store.add('User', data) + const user = store.findFirstOrThrow('User') as Document + + expect(getDocumentType(user)).toEqual('User') + }) +}) diff --git a/tests/units/document.test.ts b/tests/units/document.test.ts deleted file mode 100644 index 9473d1d..0000000 --- a/tests/units/document.test.ts +++ /dev/null @@ -1,154 +0,0 @@ -import { Store } from '../../src' -import { getDocumentType, isDocument } from '../../src/document' -import { Document } from '../../src/types' -import { User, schema, TypesMap } from '../fixtures' - -const store = new Store({ - schema, -}) - -afterEach(() => store.reset()) - -describe('`isDocument`', () => { - it('should return `true` if the input is document', () => { - const data: User = { - id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', - username: 'john.doe', - email: 'john.doe@example.com', - profile: { - username: 'john.doe', - bio: 'Excepturz.', - avatar: { - url: 'https://example.com/avatar.png', - height: 680, - width: 970, - size: '226kb', - }, - followers: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - following: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - posts: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - joinedAt: '2022-11-08T15:34:44.339Z', - }, - } - - store.add('User', data) - const user = store.findFirst('User') - - expect(isDocument(user)).toEqual(true) - }) - - it('should return `false` if the input is not a document', () => { - const data: User = { - id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', - username: 'john.doe', - email: 'john.doe@example.com', - profile: { - username: 'john.doe', - bio: 'Excepturz.', - avatar: { - url: 'https://example.com/avatar.png', - height: 680, - width: 970, - size: '226kb', - }, - followers: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - following: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - posts: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - joinedAt: '2022-11-08T15:34:44.339Z', - }, - } - - expect(isDocument(data)).toEqual(false) - }) -}) -describe('`getDocumentType`', () => { - it('should get the document type', () => { - const data: User = { - id: '7c280f0a-c1e7-4982-a008-99d9e1bcbea0', - username: 'john.doe', - email: 'john.doe@example.com', - profile: { - username: 'john.doe', - bio: 'Excepturz.', - avatar: { - url: 'https://example.com/avatar.png', - height: 680, - width: 970, - size: '226kb', - }, - followers: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - following: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - posts: { - count: 0, - edges: [], - pageInfo: { - hasNextPage: false, - hasPreviousPage: false, - }, - }, - joinedAt: '2022-11-08T15:34:44.339Z', - }, - } - - store.add('User', data) - const user = store.findFirstOrThrow('User') as Document - - expect(getDocumentType(user)).toEqual('User') - }) -}) From 3a8805c0143cc5285901a997331bc792335194fe Mon Sep 17 00:00:00 2001 From: Sayed Ali Sharifian Date: Mon, 5 Dec 2022 17:28:12 +0330 Subject: [PATCH 5/5] refactor: change instance creation on test --- tests/unit/document.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/document.test.ts b/tests/unit/document.test.ts index 0e45bd1..3f3ad15 100644 --- a/tests/unit/document.test.ts +++ b/tests/unit/document.test.ts @@ -1,10 +1,10 @@ -import { Store } from '../../src' +import { createStore } from '../../src' import { getDocumentType, isDocument } from '../../src/document' import { Document } from '../../src/types' import { User, schema, TypesMap } from '../fixtures' import { userFactory } from '../utils/factories' -const store = new Store({ +const store = createStore({ schema, })