Skip to content

Commit

Permalink
fix(jest): increase test coverage for utils
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Mar 12, 2024
1 parent 44aea7b commit 6e5bb19
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/utils/__tests__/formattedTime.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { formattedTime } from '../formattedTime.ts'

const TIME = (61 * 60 + 5) * 1000 // 1 hour, 1 minute, 5 seconds in ms

describe('formattedTime', () => {
it('should return the formatted time with optional spacing and padded minutes / seconds', () => {
const result = formattedTime(TIME)
expect(result).toBe('1 : 01 : 05')
const resultCondensed = formattedTime(TIME, true)
expect(resultCondensed).toBe('1:01:05')
})

it('should return fallback string when time value is falsy', () => {
const result = formattedTime(0)
expect(result).toBe('-- : --')
const resultCondensed = formattedTime(0, true)
expect(resultCondensed).toBe('--:--')
})
})
41 changes: 41 additions & 0 deletions src/utils/__tests__/getItemTypeFromMessage.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { SHARED_ITEM } from '../../constants.js'
import { getItemTypeFromMessage } from '../getItemTypeFromMessage.ts'

describe('getItemTypeFromMessage', () => {
it('should return the correct item type for a messages', () => {
const messages = {
1: { messageType: 'comment', messageParameters: { object: { type: 'geo-location' } } },
2: { messageType: 'comment', messageParameters: { object: { type: 'deck-card' } } },
3: { messageType: 'comment', messageParameters: { object: { type: 'talk-poll' } } },
4: { messageType: 'comment', messageParameters: { object: { type: 'some-type' } } },
5: { messageType: 'record-audio', messageParameters: { file: { mimetype: 'audio/mp3' } } },
6: { messageType: 'record-video', messageParameters: { file: { mimetype: 'video/mp4' } } },
7: { messageType: 'voice-message', messageParameters: { file: { mimetype: 'audio/mp3' } } },
8: { messageType: 'comment', messageParameters: { file: { mimetype: 'audio/mp3' } } },
9: { messageType: 'comment', messageParameters: { file: { mimetype: 'image/jpg' } } },
10: { messageType: 'comment', messageParameters: { file: { mimetype: 'video/mp4' } } },
11: { messageType: 'comment', messageParameters: { file: { mimetype: 'text/markdown' } } },
12: { messageType: 'comment', message: 'simple message' },
}

const outputTypes = {
1: SHARED_ITEM.TYPES.LOCATION,
2: SHARED_ITEM.TYPES.DECK_CARD,
3: SHARED_ITEM.TYPES.POLL,
4: SHARED_ITEM.TYPES.OTHER,
5: SHARED_ITEM.TYPES.RECORDING,
6: SHARED_ITEM.TYPES.RECORDING,
7: SHARED_ITEM.TYPES.VOICE,
8: SHARED_ITEM.TYPES.AUDIO,
9: SHARED_ITEM.TYPES.MEDIA,
10: SHARED_ITEM.TYPES.MEDIA,
11: SHARED_ITEM.TYPES.FILE,
12: SHARED_ITEM.TYPES.OTHER,
}

for (const i in messages) {
const type = i + ': ' + getItemTypeFromMessage(messages[i])
expect(type).toBe(i + ': ' + outputTypes[i])
}
})
})
63 changes: 63 additions & 0 deletions src/utils/__tests__/handleUrl.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { showError, showSuccess } from '@nextcloud/dialogs'

import {
generateAbsoluteUrl,
generateFullConversationLink,
copyConversationLinkToClipboard,
} from '../handleUrl.ts'

jest.mock('@nextcloud/dialogs', () => ({
showSuccess: jest.fn(),
showError: jest.fn(),
}))

describe('handleUrl', () => {
describe('generateAbsoluteUrl', () => {
it('should generate url with IS_DESKTOP=false correctly', () => {
const output = generateAbsoluteUrl('/path')
expect(output).toBe('http://localhost/nc-webroot/path')
})

it('should generate url with IS_DESKTOP=true correctly', () => {
const originalIsDesktop = global.IS_DESKTOP
global.IS_DESKTOP = true

const output = generateAbsoluteUrl('/path')
expect(output).toBe('/nc-webroot/path')

global.IS_DESKTOP = originalIsDesktop
})
})

describe('generateFullConversationLink', () => {
it('should generate links with given token', () => {
const link = generateFullConversationLink('TOKEN')
expect(link).toBe('http://localhost/nc-webroot/call/TOKEN')
})

it('should generate links with given token and message id', () => {
const link = generateFullConversationLink('TOKEN', '123')
expect(link).toBe('http://localhost/nc-webroot/call/TOKEN#message_123')
})
})

describe('copyConversationLinkToClipboard', () => {
it('should copy the conversation link and show success message', async () => {
Object.assign(navigator, { clipboard: { writeText: jest.fn().mockResolvedValueOnce() } })

await copyConversationLinkToClipboard('TOKEN', '123')

expect(navigator.clipboard.writeText).toHaveBeenCalledWith('http://localhost/nc-webroot/call/TOKEN#message_123')
expect(showSuccess).toHaveBeenCalled()
})

it('should show error message when copying fails', async () => {
Object.assign(navigator, { clipboard: { writeText: jest.fn().mockRejectedValueOnce() } })

await copyConversationLinkToClipboard('TOKEN', '123')

expect(navigator.clipboard.writeText).toHaveBeenCalledWith('http://localhost/nc-webroot/call/TOKEN#message_123')
expect(showError).toHaveBeenCalled()
})
})
})
57 changes: 57 additions & 0 deletions src/utils/__tests__/readableNumber.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { readableNumber, stringChop } from '../readableNumber.ts'

describe('readableNumber', () => {
describe('stringChop', () => {
it('should return the correct array of numbers', () => {
const numbers = {
1: { number: '123456789', size: 3 },
2: { number: '12345678', size: 3 },
3: { number: '1234567', size: 3 },
4: { number: '123456', size: 2 },
5: { number: '123456', size: 1 },
6: { number: '123456', size: 0 },
7: { number: '', size: 3 },
}

const outputTypes = {
1: ['123', '456', '789'],
2: ['123', '456', '78'],
3: ['123', '456', '7'],
4: ['12', '34', '56'],
5: ['1', '2', '3', '4', '5', '6'],
6: '123456',
7: '',
}

for (const i in numbers) {
const output = i + ': ' + stringChop(numbers[i].number, numbers[i].size)
expect(output).toBe(i + ': ' + outputTypes[i])
}
})
})

describe('readableNumber', () => {
it('should return the correct readable number', () => {
const numbers = {
1: 123456789,
2: '123456789',
3: '12345678',
4: '1234567',
5: '',
}

const outputTypes = {
1: '123 456 789',
2: '123 456 789',
3: '123 456 78',
4: '123 4567',
5: '',
}

for (const i in numbers) {
const output = i + ': ' + readableNumber(numbers[i])
expect(output).toBe(i + ': ' + outputTypes[i])
}
})
})
})

0 comments on commit 6e5bb19

Please sign in to comment.