diff options
Diffstat (limited to '__tests__/state')
-rw-r--r-- | __tests__/state/models/link-metas-view.test.ts | 72 | ||||
-rw-r--r-- | __tests__/state/models/log.test.ts | 153 | ||||
-rw-r--r-- | __tests__/state/models/me.test.ts | 180 | ||||
-rw-r--r-- | __tests__/state/models/navigation.test.ts | 116 | ||||
-rw-r--r-- | __tests__/state/models/root-store.test.ts | 59 | ||||
-rw-r--r-- | __tests__/state/models/shell-ui.test.ts | 61 |
6 files changed, 61 insertions, 580 deletions
diff --git a/__tests__/state/models/link-metas-view.test.ts b/__tests__/state/models/link-metas-view.test.ts deleted file mode 100644 index 0e5fb8da5..000000000 --- a/__tests__/state/models/link-metas-view.test.ts +++ /dev/null @@ -1,72 +0,0 @@ -import {RootStoreModel} from '../../../src/state/models/root-store' -import {LinkMetasViewModel} from '../../../src/state/models/link-metas-view' -import * as LinkMetaLib from '../../../src/lib/link-meta' -import {LikelyType} from './../../../src/lib/link-meta' -import {sessionClient, SessionServiceClient} from '@atproto/api' -import {DEFAULT_SERVICE} from '../../../src/state' - -describe('LinkMetasViewModel', () => { - let viewModel: LinkMetasViewModel - let rootStore: RootStoreModel - - const getLinkMetaMockSpy = jest.spyOn(LinkMetaLib, 'getLinkMeta') - const mockedMeta = { - title: 'Test Title', - url: 'testurl', - likelyType: LikelyType.Other, - } - - beforeEach(() => { - const api = sessionClient.service(DEFAULT_SERVICE) as SessionServiceClient - rootStore = new RootStoreModel(api) - viewModel = new LinkMetasViewModel(rootStore) - }) - - afterAll(() => { - jest.clearAllMocks() - }) - - describe('getLinkMeta', () => { - it('should return link meta if it is cached', async () => { - const url = 'http://example.com' - - viewModel.cache.set(url, mockedMeta) - - const result = await viewModel.getLinkMeta(url) - - expect(getLinkMetaMockSpy).not.toHaveBeenCalled() - expect(result).toEqual(mockedMeta) - }) - - it('should return link meta if it is not cached', async () => { - getLinkMetaMockSpy.mockResolvedValueOnce(mockedMeta) - - const result = await viewModel.getLinkMeta(mockedMeta.url) - - expect(getLinkMetaMockSpy).toHaveBeenCalledWith(rootStore, mockedMeta.url) - expect(result).toEqual(mockedMeta) - }) - - it('should cache the link meta if it is successfully returned', async () => { - getLinkMetaMockSpy.mockResolvedValueOnce(mockedMeta) - - await viewModel.getLinkMeta(mockedMeta.url) - - expect(viewModel.cache.get(mockedMeta.url)).toEqual(mockedMeta) - }) - - it('should not cache the link meta if it fails to return', async () => { - const url = 'http://example.com' - const error = new Error('Failed to fetch link meta') - getLinkMetaMockSpy.mockRejectedValueOnce(error) - - try { - await viewModel.getLinkMeta(url) - fail('Error was not thrown') - } catch (e) { - expect(e).toEqual(error) - expect(viewModel.cache.get(url)).toBeUndefined() - } - }) - }) -}) diff --git a/__tests__/state/models/log.test.ts b/__tests__/state/models/log.test.ts deleted file mode 100644 index b5a6d0db0..000000000 --- a/__tests__/state/models/log.test.ts +++ /dev/null @@ -1,153 +0,0 @@ -import {LogModel} from '../../../src/state/models/log' - -describe('LogModel', () => { - let logModel: LogModel - - beforeEach(() => { - logModel = new LogModel() - jest.spyOn(console, 'debug') - }) - - afterAll(() => { - jest.clearAllMocks() - }) - - it('should call a log method and add a log entry to the entries array', () => { - logModel.debug('Test log') - expect(logModel.entries.length).toEqual(1) - expect(logModel.entries[0]).toEqual({ - id: logModel.entries[0].id, - type: 'debug', - summary: 'Test log', - details: undefined, - ts: logModel.entries[0].ts, - }) - - logModel.warn('Test log') - expect(logModel.entries.length).toEqual(2) - expect(logModel.entries[1]).toEqual({ - id: logModel.entries[1].id, - type: 'warn', - summary: 'Test log', - details: undefined, - ts: logModel.entries[1].ts, - }) - - logModel.error('Test log') - expect(logModel.entries.length).toEqual(3) - expect(logModel.entries[2]).toEqual({ - id: logModel.entries[2].id, - type: 'error', - summary: 'Test log', - details: undefined, - ts: logModel.entries[2].ts, - }) - }) - - it('should call the console.debug after calling the debug method', () => { - logModel.debug('Test log') - expect(console.debug).toHaveBeenCalledWith('Test log', '') - }) - - it('should call the serialize method', () => { - logModel.debug('Test log') - expect(logModel.serialize()).toEqual({ - entries: [ - { - id: logModel.entries[0].id, - type: 'debug', - summary: 'Test log', - details: undefined, - ts: logModel.entries[0].ts, - }, - ], - }) - }) - - it('should call the hydrate method with valid properties', () => { - logModel.hydrate({ - entries: [ - { - id: '123', - type: 'debug', - summary: 'Test log', - details: undefined, - ts: 123, - }, - ], - }) - expect(logModel.entries).toEqual([ - { - id: '123', - type: 'debug', - summary: 'Test log', - details: undefined, - ts: 123, - }, - ]) - }) - - it('should call the hydrate method with invalid properties', () => { - logModel.hydrate({ - entries: [ - { - id: '123', - type: 'debug', - summary: 'Test log', - details: undefined, - ts: 123, - }, - { - summary: 'Invalid entry', - }, - ], - }) - expect(logModel.entries).toEqual([ - { - id: '123', - type: 'debug', - summary: 'Test log', - details: undefined, - ts: 123, - }, - ]) - }) - - it('should stringify the details if it is not a string', () => { - logModel.debug('Test log', {details: 'test'}) - expect(logModel.entries[0].details).toEqual('{\n "details": "test"\n}') - }) - - it('should stringify the details object if it is of a specific error', () => { - class TestError extends Error { - constructor() { - super() - this.name = 'TestError' - } - } - const error = new TestError() - logModel.error('Test error log', error) - expect(logModel.entries[0].details).toEqual('TestError') - - class XRPCInvalidResponseErrorMock { - validationError = {toString: () => 'validationError'} - lexiconNsid = 'test' - } - const xrpcInvalidResponseError = new XRPCInvalidResponseErrorMock() - logModel.error('Test error log', xrpcInvalidResponseError) - expect(logModel.entries[1].details).toEqual( - '{\n "validationError": {},\n "lexiconNsid": "test"\n}', - ) - - class XRPCErrorMock { - status = 'status' - error = 'error' - message = 'message' - } - const xrpcError = new XRPCErrorMock() - logModel.error('Test error log', xrpcError) - expect(logModel.entries[2].details).toEqual( - '{\n "status": "status",\n "error": "error",\n "message": "message"\n}', - ) - }) -}) diff --git a/__tests__/state/models/me.test.ts b/__tests__/state/models/me.test.ts deleted file mode 100644 index b69e43477..000000000 --- a/__tests__/state/models/me.test.ts +++ /dev/null @@ -1,180 +0,0 @@ -import {RootStoreModel} from '../../../src/state/models/root-store' -import {MeModel} from '../../../src/state/models/me' -import {NotificationsViewModel} from './../../../src/state/models/notifications-view' -import {sessionClient, SessionServiceClient} from '@atproto/api' -import {DEFAULT_SERVICE} from './../../../src/state/index' - -describe('MeModel', () => { - let rootStore: RootStoreModel - let meModel: MeModel - - beforeEach(() => { - const api = sessionClient.service(DEFAULT_SERVICE) as SessionServiceClient - rootStore = new RootStoreModel(api) - meModel = new MeModel(rootStore) - }) - - afterAll(() => { - jest.clearAllMocks() - }) - - it('should clear() correctly', () => { - meModel.did = '123' - meModel.handle = 'handle' - meModel.displayName = 'John Doe' - meModel.description = 'description' - meModel.avatar = 'avatar' - meModel.notificationCount = 1 - meModel.clear() - expect(meModel.did).toEqual('') - expect(meModel.handle).toEqual('') - expect(meModel.displayName).toEqual('') - expect(meModel.description).toEqual('') - expect(meModel.avatar).toEqual('') - expect(meModel.notificationCount).toEqual(0) - }) - - it('should hydrate() successfully with valid properties', () => { - meModel.hydrate({ - did: '123', - handle: 'handle', - displayName: 'John Doe', - description: 'description', - avatar: 'avatar', - }) - expect(meModel.did).toEqual('123') - expect(meModel.handle).toEqual('handle') - expect(meModel.displayName).toEqual('John Doe') - expect(meModel.description).toEqual('description') - expect(meModel.avatar).toEqual('avatar') - }) - - it('should not hydrate() with invalid properties', () => { - meModel.hydrate({ - did: '', - handle: 'handle', - displayName: 'John Doe', - description: 'description', - avatar: 'avatar', - }) - expect(meModel.did).toEqual('') - expect(meModel.handle).toEqual('') - expect(meModel.displayName).toEqual('') - expect(meModel.description).toEqual('') - expect(meModel.avatar).toEqual('') - - meModel.hydrate({ - did: '123', - displayName: 'John Doe', - description: 'description', - avatar: 'avatar', - }) - expect(meModel.did).toEqual('') - expect(meModel.handle).toEqual('') - expect(meModel.displayName).toEqual('') - expect(meModel.description).toEqual('') - expect(meModel.avatar).toEqual('') - }) - - it('should load() successfully', async () => { - jest - .spyOn(rootStore.api.app.bsky.actor, 'getProfile') - .mockImplementationOnce((): Promise<any> => { - return Promise.resolve({ - data: { - displayName: 'John Doe', - description: 'description', - avatar: 'avatar', - }, - }) - }) - rootStore.session.data = { - did: '123', - handle: 'handle', - service: 'test service', - accessJwt: 'test token', - refreshJwt: 'test token', - } - await meModel.load() - expect(meModel.did).toEqual('123') - expect(meModel.handle).toEqual('handle') - expect(meModel.displayName).toEqual('John Doe') - expect(meModel.description).toEqual('description') - expect(meModel.avatar).toEqual('avatar') - }) - - it('should load() successfully without profile data', async () => { - jest - .spyOn(rootStore.api.app.bsky.actor, 'getProfile') - .mockImplementationOnce((): Promise<any> => { - return Promise.resolve({ - data: null, - }) - }) - rootStore.session.data = { - did: '123', - handle: 'handle', - service: 'test service', - accessJwt: 'test token', - refreshJwt: 'test token', - } - await meModel.load() - expect(meModel.did).toEqual('123') - expect(meModel.handle).toEqual('handle') - expect(meModel.displayName).toEqual('') - expect(meModel.description).toEqual('') - expect(meModel.avatar).toEqual('') - }) - - it('should load() to nothing when no session', async () => { - rootStore.session.data = null - await meModel.load() - expect(meModel.did).toEqual('') - expect(meModel.handle).toEqual('') - expect(meModel.displayName).toEqual('') - expect(meModel.description).toEqual('') - expect(meModel.avatar).toEqual('') - expect(meModel.notificationCount).toEqual(0) - }) - - it('should serialize() key information', () => { - meModel.did = '123' - meModel.handle = 'handle' - meModel.displayName = 'John Doe' - meModel.description = 'description' - meModel.avatar = 'avatar' - - expect(meModel.serialize()).toEqual({ - did: '123', - handle: 'handle', - displayName: 'John Doe', - description: 'description', - avatar: 'avatar', - }) - }) - - it('should clearNotificationCount() successfully', () => { - meModel.clearNotificationCount() - expect(meModel.notificationCount).toBe(0) - }) - - it('should update notifs count with fetchStateUpdate()', async () => { - meModel.notifications = { - refresh: jest.fn().mockResolvedValue({}), - } as unknown as NotificationsViewModel - - jest - .spyOn(rootStore.api.app.bsky.notification, 'getCount') - .mockImplementationOnce((): Promise<any> => { - return Promise.resolve({ - data: { - count: 1, - }, - }) - }) - - await meModel.fetchNotifications() - expect(meModel.notificationCount).toBe(1) - expect(meModel.notifications.refresh).toHaveBeenCalled() - }) -}) diff --git a/__tests__/state/models/navigation.test.ts b/__tests__/state/models/navigation.test.ts index 80f66d494..16a20206c 100644 --- a/__tests__/state/models/navigation.test.ts +++ b/__tests__/state/models/navigation.test.ts @@ -1,11 +1,16 @@ +import {RootStoreModel} from './../../../src/state/models/root-store' import {NavigationModel} from './../../../src/state/models/navigation' -import * as flags from '../../../src/build-flags' +import * as flags from '../../../src/lib/build-flags' +import AtpAgent from '@atproto/api' +import {DEFAULT_SERVICE} from '../../../src/state' describe('NavigationModel', () => { let model: NavigationModel + let rootStore: RootStoreModel beforeEach(() => { - model = new NavigationModel() + rootStore = new RootStoreModel(new AtpAgent({service: DEFAULT_SERVICE})) + model = new NavigationModel(rootStore) model.setTitle('0-0', 'title') }) @@ -15,7 +20,7 @@ describe('NavigationModel', () => { it('should clear() to the correct base state', async () => { await model.clear() - expect(model.tabCount).toBe(2) + expect(model.tabCount).toBe(3) expect(model.tab).toEqual({ fixedTabPurpose: 0, history: [ @@ -64,7 +69,7 @@ describe('NavigationModel', () => { }) it('should call the tabCount getter', () => { - expect(model.tabCount).toBe(2) + expect(model.tabCount).toBe(3) }) describe('tabs not enabled', () => { @@ -87,7 +92,7 @@ describe('NavigationModel', () => { it('should not change the active tab', () => { // @ts-expect-error flags.TABS_ENABLED = false - model.setActiveTab(2) + model.setActiveTab(3) expect(model.tabIndex).toBe(0) }) @@ -95,57 +100,58 @@ describe('NavigationModel', () => { // @ts-expect-error flags.TABS_ENABLED = false model.closeTab(0) - expect(model.tabCount).toBe(2) + expect(model.tabCount).toBe(3) }) }) - describe('tabs enabled', () => { - jest.mock('../../../src/build-flags', () => ({ - TABS_ENABLED: true, - })) - - afterAll(() => { - jest.clearAllMocks() - }) - - it('should create new tabs', () => { - // @ts-expect-error - flags.TABS_ENABLED = true - - model.newTab('testurl', 'title') - expect(model.tab.isNewTab).toBe(true) - expect(model.tabIndex).toBe(2) - }) - - it('should change the current tab', () => { - // @ts-expect-error - flags.TABS_ENABLED = true - - model.setActiveTab(0) - expect(model.tabIndex).toBe(0) - }) - - it('should close tabs', () => { - // @ts-expect-error - flags.TABS_ENABLED = true - - model.closeTab(0) - expect(model.tabs).toEqual([ - { - fixedTabPurpose: 1, - history: [ - { - id: expect.anything(), - ts: expect.anything(), - url: '/notifications', - }, - ], - id: expect.anything(), - index: 0, - isNewTab: false, - }, - ]) - expect(model.tabIndex).toBe(0) - }) - }) + // TODO restore when tabs get re-enabled + // describe('tabs enabled', () => { + // jest.mock('../../../src/build-flags', () => ({ + // TABS_ENABLED: true, + // })) + + // afterAll(() => { + // jest.clearAllMocks() + // }) + + // it('should create new tabs', () => { + // // @ts-expect-error + // flags.TABS_ENABLED = true + + // model.newTab('testurl', 'title') + // expect(model.tab.isNewTab).toBe(true) + // expect(model.tabIndex).toBe(2) + // }) + + // it('should change the current tab', () => { + // // @ts-expect-error + // flags.TABS_ENABLED = true + + // model.setActiveTab(0) + // expect(model.tabIndex).toBe(0) + // }) + + // it('should close tabs', () => { + // // @ts-expect-error + // flags.TABS_ENABLED = true + + // model.closeTab(0) + // expect(model.tabs).toEqual([ + // { + // fixedTabPurpose: 1, + // history: [ + // { + // id: expect.anything(), + // ts: expect.anything(), + // url: '/notifications', + // }, + // ], + // id: expect.anything(), + // index: 0, + // isNewTab: false, + // }, + // ]) + // expect(model.tabIndex).toBe(0) + // }) + // }) }) diff --git a/__tests__/state/models/root-store.test.ts b/__tests__/state/models/root-store.test.ts deleted file mode 100644 index 56eec9cdf..000000000 --- a/__tests__/state/models/root-store.test.ts +++ /dev/null @@ -1,59 +0,0 @@ -import {RootStoreModel} from '../../../src/state/models/root-store' -import {setupState} from '../../../src/state' - -describe('rootStore', () => { - let rootStore: RootStoreModel - - beforeAll(() => { - jest.useFakeTimers() - }) - - beforeEach(async () => { - rootStore = await setupState() - }) - - afterAll(() => { - jest.clearAllMocks() - }) - - it('should call the clearAll() resets state correctly', () => { - rootStore.clearAll() - - expect(rootStore.session.data).toEqual(null) - expect(rootStore.nav.tabs).toEqual([ - { - fixedTabPurpose: 0, - history: [ - { - id: expect.anything(), - ts: expect.anything(), - url: '/', - }, - ], - id: expect.anything(), - index: 0, - isNewTab: false, - }, - { - fixedTabPurpose: 1, - history: [ - { - id: expect.anything(), - ts: expect.anything(), - url: '/notifications', - }, - ], - id: expect.anything(), - index: 0, - isNewTab: false, - }, - ]) - expect(rootStore.nav.tabIndex).toEqual(0) - expect(rootStore.me.did).toEqual('') - expect(rootStore.me.handle).toEqual('') - expect(rootStore.me.displayName).toEqual('') - expect(rootStore.me.description).toEqual('') - expect(rootStore.me.avatar).toEqual('') - expect(rootStore.me.notificationCount).toEqual(0) - }) -}) diff --git a/__tests__/state/models/shell-ui.test.ts b/__tests__/state/models/shell-ui.test.ts deleted file mode 100644 index b6ccd064a..000000000 --- a/__tests__/state/models/shell-ui.test.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { - ConfirmModal, - ImagesLightbox, - ShellUiModel, -} from './../../../src/state/models/shell-ui' - -describe('ShellUiModel', () => { - let model: ShellUiModel - - beforeEach(() => { - model = new ShellUiModel() - }) - - afterAll(() => { - jest.clearAllMocks() - }) - - it('should call the openModal & closeModal method', () => { - const m = new ConfirmModal('Test Modal', 'Look good?', () => {}) - model.openModal(m) - expect(model.isModalActive).toEqual(true) - expect(model.activeModal).toEqual(m) - - model.closeModal() - expect(model.isModalActive).toEqual(false) - expect(model.activeModal).toBeUndefined() - }) - - it('should call the openLightbox & closeLightbox method', () => { - const lt = new ImagesLightbox(['uri'], 0) - model.openLightbox(lt) - expect(model.isLightboxActive).toEqual(true) - expect(model.activeLightbox).toEqual(lt) - - model.closeLightbox() - expect(model.isLightboxActive).toEqual(false) - expect(model.activeLightbox).toBeUndefined() - }) - - it('should call the openComposer & closeComposer method', () => { - const composer = { - replyTo: { - uri: 'uri', - cid: 'cid', - text: 'text', - author: { - handle: 'handle', - displayName: 'name', - }, - }, - onPost: jest.fn(), - } - model.openComposer(composer) - expect(model.isComposerActive).toEqual(true) - expect(model.composerOpts).toEqual(composer) - - model.closeComposer() - expect(model.isComposerActive).toEqual(false) - expect(model.composerOpts).toBeUndefined() - }) -}) |