From 358417dd7b57fcf535fb374ff0e6d07b2a3f9929 Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Thu, 29 Aug 2024 00:03:25 +0800 Subject: [PATCH] add unit tests to app/utils/permalink (#8141) --- app/utils/permalink/index.test.ts | 84 +++++++++++++++++++++++++++++++ app/utils/permalink/index.ts | 1 + 2 files changed, 85 insertions(+) create mode 100644 app/utils/permalink/index.test.ts diff --git a/app/utils/permalink/index.test.ts b/app/utils/permalink/index.test.ts new file mode 100644 index 000000000..add8265bd --- /dev/null +++ b/app/utils/permalink/index.test.ts @@ -0,0 +1,84 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import {Keyboard, Platform} from 'react-native'; +import {OptionsModalPresentationStyle} from 'react-native-navigation'; + +import {dismissAllModals, showModalOverCurrentContext} from '@screens/navigation'; + +import {displayPermalink, closePermalink} from '.'; + +jest.mock('@screens/navigation', () => ({ + dismissAllModals: jest.fn(), + showModalOverCurrentContext: jest.fn(), +})); + +describe('permalinkUtils', () => { + const originalSelect = Platform.select; + + beforeAll(() => { + Platform.select = ({android, ios, default: dft}: any) => { + if (Platform.OS === 'android' && android) { + return android; + } else if (Platform.OS === 'ios' && ios) { + return ios; + } + + return dft; + }; + }); + + afterAll(() => { + Platform.select = originalSelect; + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + describe('displayPermalink', () => { + it('should dismiss keyboard and show permalink modal', async () => { + const dismiss = jest.spyOn(Keyboard, 'dismiss'); + await displayPermalink('teamName', 'postId'); + expect(dismiss).toHaveBeenCalled(); + expect(showModalOverCurrentContext).toHaveBeenCalledWith( + 'Permalink', + {isPermalink: true, teamName: 'teamName', postId: 'postId'}, + { + modalPresentationStyle: OptionsModalPresentationStyle.overFullScreen, + layout: { + componentBackgroundColor: 'rgba(0,0,0,0.2)', + }, + }, + ); + }); + + it('should dismiss all modals if showingPermalink is true', async () => { + // Simulate showingPermalink being true + await displayPermalink('teamName', 'postId'); + await displayPermalink('teamName', 'postId'); + expect(dismissAllModals).toHaveBeenCalled(); + }); + + it('should handle platform specific options correctly', async () => { + await displayPermalink('teamName', 'postId'); + expect(showModalOverCurrentContext).toHaveBeenCalledWith( + 'Permalink', + {isPermalink: true, teamName: 'teamName', postId: 'postId'}, + { + modalPresentationStyle: OptionsModalPresentationStyle.overFullScreen, + layout: { + componentBackgroundColor: 'rgba(0,0,0,0.2)', + }, + }, + ); + }); + }); + + describe('closePermalink', () => { + it('should set showingPermalink to false', () => { + const showingPermalink = closePermalink(); + expect(showingPermalink).toBe(false); + }); + }); +}); diff --git a/app/utils/permalink/index.ts b/app/utils/permalink/index.ts index b52f1dba2..6af509fc3 100644 --- a/app/utils/permalink/index.ts +++ b/app/utils/permalink/index.ts @@ -39,4 +39,5 @@ export const displayPermalink = async (teamName: string, postId: string, openAsP export const closePermalink = () => { showingPermalink = false; + return showingPermalink; };