Skip to content

Commit

Permalink
Adding initial support for typescript tests (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jespino committed May 28, 2020
1 parent 38dafa0 commit b848769
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
4 changes: 3 additions & 1 deletion webapp/package.json
Expand Up @@ -41,6 +41,7 @@
"jest-canvas-mock": "2.2.0",
"jest-junit": "10.0.0",
"mattermost-webapp": "github:mattermost/mattermost-webapp#23f5f93d9f12a7e2b5623e5cee6814366abd9a0f",
"ts-jest": "26.0.0",
"ts-loader": "7.0.5",
"webpack": "4.35.0",
"webpack-bundle-analyzer": "3.8.0",
Expand All @@ -55,6 +56,7 @@
"typescript": "3.5.3"
},
"jest": {
"preset": "ts-jest",
"snapshotSerializers": [
"<rootDir>/node_modules/enzyme-to-json/serializer"
],
Expand All @@ -64,7 +66,7 @@
],
"clearMocks": true,
"collectCoverageFrom": [
"src/**/*.{js,jsx}"
"src/**/*.{ts,tsx}"
],
"coverageReporters": [
"lcov",
Expand Down
79 changes: 79 additions & 0 deletions webapp/src/utils/user_utils.test.ts
@@ -0,0 +1,79 @@
import {UserProfile} from 'mattermost-redux/types/users';

import {displayUsernameForUser} from './user_utils';

describe('displayUsernameForUser', () => {
const defaultConfig = {
TeammateNameDisplay: 'nickname_full_name'
};

const user: UserProfile = {
id: 'test',
create_at: 0,
update_at: 0,
delete_at: 0,
auth_data: '',
auth_service: '',
email: '',
position: '',
roles: '',
locale: '',
notify_props: {
desktop: 'all',
desktop_sound: 'true',
email: 'true',
mark_unread: 'all',
push: 'all',
push_status: 'online',
comments: 'any',
first_name: 'true',
channel: 'true',
mention_keys: ''
},
email_verified: true,
terms_of_service_id: '',
terms_of_service_create_at: 0,
is_bot: false,
last_picture_update: 0,
username: 'test-username',
first_name: 'test-first-name',
last_name: 'test-last-name',
nickname: 'test-nickname'
};

it('should return and empty string is there is no user', () => {
expect(displayUsernameForUser(null, defaultConfig)).toBe('');
});

it('should return the username if no config is passed', () => {
expect(displayUsernameForUser(user, {})).toBe(user.username);
});

it('should return the nickname if nickname_full_name config is passed and there is a nickname', () => {
expect(displayUsernameForUser(user, {TeammateNameDisplay: 'nickname_full_name'})).toBe(user.nickname);
});

it('should return the full name if nickname_full_name config is passed and there is not a nickname', () => {
expect(displayUsernameForUser({...user, nickname: ''}, {TeammateNameDisplay: 'nickname_full_name'})).toBe(`${user.first_name} ${user.last_name}`);
});

it('should return the username if nickname_full_name config is passed and there is not a nickname, first_name or last_name', () => {
expect(displayUsernameForUser({...user, nickname: '', first_name: '', last_name: ''}, {TeammateNameDisplay: 'nickname_full_name'})).toBe(user.username);
});

it('should return the full name if nickname_full_name config is passed and there is not a nickname', () => {
expect(displayUsernameForUser({...user, nickname: ''}, {TeammateNameDisplay: 'nickname_full_name'})).toBe(`${user.first_name} ${user.last_name}`);
});

it('should return the username if nickname_full_name config is passed and there is not a nickname, first_name or last_name', () => {
expect(displayUsernameForUser({...user, nickname: '', first_name: '', last_name: ''}, {TeammateNameDisplay: 'nickname_full_name'})).toBe(user.username);
});

it('should return the fullname if full_name config is passed and there is a full_name and a nickname', () => {
expect(displayUsernameForUser(user, {TeammateNameDisplay: 'full_name'})).toBe(`${user.first_name} ${user.last_name}`);
});

it('should return the username if full_name config is passed and there is not a first_name or last_name', () => {
expect(displayUsernameForUser({...user, first_name: '', last_name: ''}, {TeammateNameDisplay: 'full_name'})).toBe(user.username);
});
});
2 changes: 1 addition & 1 deletion webapp/src/utils/user_utils.ts
Expand Up @@ -3,7 +3,7 @@ import {UserProfile} from 'mattermost-redux/types/users';

import {Config} from '../types';

export function displayUsernameForUser(user: UserProfile, config: Config): string {
export function displayUsernameForUser(user: UserProfile | null, config: Config): string {
if (user) {
const nameFormat = config.TeammateNameDisplay;
let name = user.username;
Expand Down
1 change: 1 addition & 0 deletions webapp/tests/i18n_mock.json
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions webapp/tests/setup.js
@@ -0,0 +1,2 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

0 comments on commit b848769

Please sign in to comment.