Skip to content

Commit

Permalink
add unit tests to app/utils/permalink (#8141)
Browse files Browse the repository at this point in the history
  • Loading branch information
enahum authored Aug 28, 2024
1 parent 48af5fd commit 358417d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
84 changes: 84 additions & 0 deletions app/utils/permalink/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
1 change: 1 addition & 0 deletions app/utils/permalink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export const displayPermalink = async (teamName: string, postId: string, openAsP

export const closePermalink = () => {
showingPermalink = false;
return showingPermalink;
};

0 comments on commit 358417d

Please sign in to comment.