focalboard/webapp/src/utils.test.ts

190 lines
7.4 KiB
TypeScript
Raw Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {createIntl} from 'react-intl'
import {createMemoryHistory} from "history"
import {match as routerMatch} from "react-router-dom"
import {Utils, IDType} from './utils'
import {IAppWindow} from './types'
declare let window: IAppWindow
2021-06-15 09:34:59 +02:00
describe('utils', () => {
describe('assureProtocol', () => {
test('should passthrough on valid short protocol', () => {
expect(Utils.ensureProtocol('https://focalboard.com')).toBe('https://focalboard.com')
})
test('should passthrough on valid long protocol', () => {
expect(Utils.ensureProtocol('somecustomprotocol://focalboard.com')).toBe('somecustomprotocol://focalboard.com')
})
2021-06-15 09:34:59 +02:00
test('should passthrough on valid short protocol', () => {
expect(Utils.ensureProtocol('x://focalboard.com')).toBe('x://focalboard.com')
})
2021-06-15 09:34:59 +02:00
test('should add a https for empty protocol', () => {
expect(Utils.ensureProtocol('focalboard.com')).toBe('https://focalboard.com')
})
})
describe('createGuid', () => {
test('should create 27 char random id for workspace', () => {
expect(Utils.createGuid(IDType.Workspace)).toMatch(/^w[ybndrfg8ejkmcpqxot1uwisza345h769]{26}$/)
})
test('should create 27 char random id for board', () => {
expect(Utils.createGuid(IDType.Board)).toMatch(/^b[ybndrfg8ejkmcpqxot1uwisza345h769]{26}$/)
})
test('should create 27 char random id for card', () => {
expect(Utils.createGuid(IDType.Card)).toMatch(/^c[ybndrfg8ejkmcpqxot1uwisza345h769]{26}$/)
})
test('should create 27 char random id', () => {
expect(Utils.createGuid(IDType.None)).toMatch(/^7[ybndrfg8ejkmcpqxot1uwisza345h769]{26}$/)
})
})
2021-06-15 09:34:59 +02:00
describe('htmlFromMarkdown', () => {
test('should not allow XSS on links href on the webapp', () => {
expect(Utils.htmlFromMarkdown('[]("xss-attack="true"other="whatever)')).toBe('<p><a target="_blank" rel="noreferrer" href="%22xss-attack=%22true%22other=%22whatever" title="" onclick=""></a></p>')
2021-06-15 09:34:59 +02:00
})
test('should not allow XSS on links href on the desktop app', () => {
window.openInNewBrowser = () => null
const expectedHtml = '<p><a target="_blank" rel="noreferrer" href="%22xss-attack=%22true%22other=%22whatever" title="" onclick=" openInNewBrowser && openInNewBrowser(event.target.href);"></a></p>'
2021-06-15 09:34:59 +02:00
expect(Utils.htmlFromMarkdown('[]("xss-attack="true"other="whatever)')).toBe(expectedHtml)
window.openInNewBrowser = null
2021-06-15 09:34:59 +02:00
})
})
2021-06-22 17:26:00 +02:00
describe('countCheckboxesInMarkdown', () => {
test('should count checkboxes', () => {
const text = `
## Header
- [x] one
- [ ] two
- [x] three
`.replace(/\n\s+/gm, '\n')
const checkboxes = Utils.countCheckboxesInMarkdown(text)
expect(checkboxes.total).toBe(3)
expect(checkboxes.checked).toBe(2)
})
})
2021-06-22 17:26:00 +02:00
describe('test - buildURL', () => {
test('buildURL, no base', () => {
Merge Onboarding feature branch into main (#2406) * Persistent user config (#2133) * Added user config API * Add unit tests * lint fix * Fixed webapp tests * Fixed webapp tests * Updated props in store after updating * Minor fixes * Removed redundent data from audit logs * Onboarding Tour (#2287) * Created private board * Roughly displayed tour * Synced with Dhama's changes * WIP * Trying to add GIF * Added 3 tour steps * WIP * WIP * WIP * checked in missed file * Synced with feature branch * WIp * Adde skip tour option * Fixed image loading for on-prem * Made tour work on presonal server: * Adde missed file * Adding telemetry * Adding telemetry * Added tour tip telemetry * Fixed pulsating dot styling for personal server * reverted personal config * Added reset tour button * Displayed share tour tip of feature is enabled * Lint fixes * Fixed webapp tests * Fixed webapp tests * Completed webapp tests * Completed webapp tests * Webapp lint fixes * Added server tests * Testing cypress skip tour fix * Fixed Cypress tests * Added share board tour step * Added share board tour step * webapp lint fixes * Updated logic to pick welcome board * Updated tests: * lint fixes * Updating UI changes * Fixed a bug causing card tour to re-appear * FIxed minor issue * FIxed bug where card tour didn't start in clickingh on card * Fixed tests * Make update user props use string instead of interface * Fixed a value type * Updating gif size * Updating resolution breakpoint * Updating tutorial tip * Updating view selector * Refactored tour components * Misc fixes * minor refactoring * GH-2258: allow date range to overflow (#2268) * allow date range to overflow * Fixed issue with date overflowing into neighbouring column Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Update readme with accurate Linux standalone app build instructions (#2351) * Bump follow-redirects from 1.14.7 to 1.14.8 in /experiments/webext (#2339) Bumps [follow-redirects](https://github.com/follow-redirects/follow-redirects) from 1.14.7 to 1.14.8. - [Release notes](https://github.com/follow-redirects/follow-redirects/releases) - [Commits](https://github.com/follow-redirects/follow-redirects/compare/v1.14.7...v1.14.8) --- updated-dependencies: - dependency-name: follow-redirects dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Switch component style fixed: selector specificity increased by adding additional class. (#2179) * Adding sever side undelete endpoint (#2222) * Adding sever side undelete endpoint * Removing long lines golangci-lint errors * Fixing linter errors * Fixing a test problem * Fixing tests Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Removing transactions from sqlite backend (#2361) * Removing transactions from sqlite backend * Skipping tests in sqlite because the lack of transactions * Generating the mocks * Fixing golangci-lint * Fixing problem opening the tour tooltip on card open * Fixing texts missmatch * Adding the Product Tour entry in the user settings menu * Fixing some tests * Fixing tests Co-authored-by: Asaad Mahmood <asaadmahmood@users.noreply.github.com> Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Doug Lauder <wiggin77@warpmail.net> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: kamre <eremchenko@gmail.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> * Restored package json * Restored package json Co-authored-by: Asaad Mahmood <asaadmahmood@users.noreply.github.com> Co-authored-by: Scott Bishel <scott.bishel@mattermost.com> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Doug Lauder <wiggin77@warpmail.net> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: kamre <eremchenko@gmail.com> Co-authored-by: Jesús Espino <jespinog@gmail.com>
2022-02-28 12:28:16 +01:00
(global as any).isFocalboardPlugin = true
2021-06-22 17:26:00 +02:00
expect(Utils.buildURL('test', true)).toBe('http://localhost/test')
expect(Utils.buildURL('/test', true)).toBe('http://localhost/test')
expect(Utils.buildURL('test')).toBe('/test')
expect(Utils.buildURL('/test')).toBe('/test')
})
test('buildURL, base no slash', () => {
window.baseURL = 'base'
2021-06-22 17:26:00 +02:00
expect(Utils.buildURL('test', true)).toBe('http://localhost/base/test')
expect(Utils.buildURL('/test', true)).toBe('http://localhost/base/test')
expect(Utils.buildURL('test')).toBe('base/test')
expect(Utils.buildURL('/test')).toBe('base/test')
})
test('buildUrl, base with slash', () => {
window.baseURL = '/base/'
2021-06-22 17:26:00 +02:00
expect(Utils.buildURL('test', true)).toBe('http://localhost/base/test')
expect(Utils.buildURL('/test', true)).toBe('http://localhost/base/test')
expect(Utils.buildURL('test')).toBe('base/test')
expect(Utils.buildURL('/test')).toBe('base/test')
})
})
describe('display date', () => {
const intl = createIntl({locale: 'en-us'})
it('should show month and day for current year', () => {
const currentYear = new Date().getFullYear()
const date = new Date(currentYear, 6, 9)
expect(Utils.displayDate(date, intl)).toBe('July 09')
})
it('should show month, day and year for previous year', () => {
const currentYear = new Date().getFullYear()
const previousYear = currentYear - 1
const date = new Date(previousYear, 6, 9)
expect(Utils.displayDate(date, intl)).toBe(`July 09, ${previousYear}`)
})
})
describe('input date', () => {
const currentYear = new Date().getFullYear()
const date = new Date(currentYear, 6, 9)
it('should show mm/dd/yyyy for current year', () => {
const intl = createIntl({locale: 'en-us'})
expect(Utils.inputDate(date, intl)).toBe(`07/09/${currentYear}`)
})
it('should show dd/mm/yyyy for current year, es local', () => {
const intl = createIntl({locale: 'es-es'})
expect(Utils.inputDate(date, intl)).toBe(`09/07/${currentYear}`)
})
})
describe('display date and time', () => {
const intl = createIntl({locale: 'en-us'})
it('should show month, day and time for current year', () => {
const currentYear = new Date().getFullYear()
const date = new Date(currentYear, 6, 9, 15, 20)
expect(Utils.displayDateTime(date, intl)).toBe('July 09, 3:20 PM')
})
it('should show month, day, year and time for previous year', () => {
const currentYear = new Date().getFullYear()
const previousYear = currentYear - 1
const date = new Date(previousYear, 6, 9, 5, 35)
expect(Utils.displayDateTime(date, intl)).toBe(`July 09, ${previousYear}, 5:35 AM`)
})
})
describe('compare versions', () => {
it('should return one if b > a', () => {
expect(Utils.compareVersions('0.9.4', '0.10.0')).toBe(1)
})
it('should return zero if a = b', () => {
expect(Utils.compareVersions('1.2.3', '1.2.3')).toBe(0)
})
it('should return minus one if b < a', () => {
expect(Utils.compareVersions('10.9.4', '10.9.2')).toBe(-1)
})
})
describe('showBoard test', () => {
it('should switch boards', () => {
const match = {
params: {
boardId: 'board_id_1',
viewId: 'view_id_1',
cardId: 'card_id_1',
teamId: 'team_id_1',
},
path: '/team/:teamId/:boardId?/:viewId?/:cardId?',
} as unknown as routerMatch<{boardId: string, viewId?: string, cardId?: string, teamId?: string}>
const history = createMemoryHistory()
history.push = jest.fn()
Utils.showBoard('board_id_2', match, history)
expect(history.push).toBeCalledWith('/team/team_id_1/board_id_2')
})
})
})