Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: back merge utils for truncate title #4697

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lang/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -2646,6 +2646,9 @@
"kCp8A9": {
"defaultMessage": "After resignation, you will not be able to manage tags."
},
"kEDrXh": {
"defaultMessage": "liked your collection"
},
"kHMa3H": {
"defaultMessage": "The new password and confirmation password do not match."
},
Expand Down
3 changes: 3 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2646,6 +2646,9 @@
"kCp8A9": {
"defaultMessage": "After resignation, you will not be able to manage tags."
},
"kEDrXh": {
"defaultMessage": "liked your collection"
},
"kHMa3H": {
"defaultMessage": "The new password and confirmation password do not match."
},
Expand Down
3 changes: 3 additions & 0 deletions lang/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -2646,6 +2646,9 @@
"kCp8A9": {
"defaultMessage": "如果辞去权限,你将无法继续管理标签。"
},
"kEDrXh": {
"defaultMessage": "喜欢你的选集"
},
"kHMa3H": {
"defaultMessage": "密码不一致"
},
Expand Down
3 changes: 3 additions & 0 deletions lang/zh-Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -2646,6 +2646,9 @@
"kCp8A9": {
"defaultMessage": "如果辭去權限,你將無法繼續管理標籤。"
},
"kEDrXh": {
"defaultMessage": "喜歡你的選集"
},
"kHMa3H": {
"defaultMessage": "密碼不一致"
},
Expand Down
1 change: 1 addition & 0 deletions src/common/enums/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export enum TEST_ID {
NOTICE_OFFICIAL_ANNOUNCEMENT = 'notice/official-announcement',
NOTICE_MOMENT_MENTIONED = 'notice/moment-mentioned',
NOTICE_MOMENT_LIKED = 'notice/moment-liked',
NOTICE_COLLECTION_TITLE = 'notice/collection/title',
// me
ME_WALLET_TRANSACTIONS_ITEM = 'me/wallet/transactions/item',
ME_WALLET_TRANSACTIONS_ITEM_AMOUNT = 'me/wallet/transactions/item/amount',
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/text/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './article'
export * from './moment'
export * from './notice'
export * from './tag'
export * from './user'

Expand Down
90 changes: 0 additions & 90 deletions src/common/utils/text/moment.test.ts

This file was deleted.

164 changes: 164 additions & 0 deletions src/common/utils/text/notice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { describe, expect, it } from 'vitest'

import { UserLanguage } from '~/gql/graphql'

import { truncateNoticeTitle } from './notice'

describe.concurrent('utils/text/collection/truncateNoticeTitle', () => {
describe('for Chinese', () => {
it('should truncate the title to the specified maximum number of words', () => {
const title = '这是一个标题这是一个标题这是一个标题'
const maxLength = 3
const expected = '这是一...'
const result = truncateNoticeTitle(title, {
locale: UserLanguage.ZhHans,
maxLength,
})
// Assert
expect(result).toEqual(expected)
})

it('should return the title as is if it has fewer words than the maximum', () => {
const title = '这是一个标题'
const maxLength = 7
const result = truncateNoticeTitle(title, {
locale: UserLanguage.ZhHans,
maxLength,
})
// Assert
expect(result).toEqual(title)
})

it('should return the title for the default length of 10 words', () => {
const title = '这是一个标题这是一个标题这是一个标题'
const expected = '这是一个标题这是一个...'
const result = truncateNoticeTitle(title, { locale: UserLanguage.ZhHans })
// Assert
expect(result).toEqual(expected)
})
})

describe('for English', () => {
it('should return the title as is if it has fewer words than the maximum', () => {
const title = 'The birds are chirping and the sun is shining'
const maxLength = 50
const result = truncateNoticeTitle(title, {
locale: UserLanguage.En,
maxLength,
})
// Assert
expect(result).toEqual(title)
})

it('should truncate the title to the specified maximum number of words', () => {
const title = 'The birds are chirping and the sun is shining'
const maxLength = 27
const expected = 'The birds are chirping and...'
const result = truncateNoticeTitle(title, {
locale: UserLanguage.En,
maxLength,
})
// Assert
expect(result).toEqual(expected)
})
})

describe('for English with tagged users', () => {
it('should truncate characters to under 10 words for english', () => {
expect(
truncateNoticeTitle('This is a very long sentence.', {
includeAtSign: true,
})
).toBe('This is a...')
expect(
truncateNoticeTitle('Hello, world.', { includeAtSign: true })
).toBe('Hello,...')
})

it('should truncate if over 10 characters with tagged users and remaining length is 0 while having english characters', () => {
expect(
truncateNoticeTitle('This is a craaaazy article here! @user1 @user2', {
includeAtSign: true,
})
).toBe('This is a...@user1 @user2')
})
})

describe('for Chinese with tagged users', () => {
it('should not truncate if under 10 characters', () => {
expect(
truncateNoticeTitle('這篇文章真的很厲害!', {
locale: UserLanguage.ZhHant,
maxLength: 10,
includeAtSign: true,
})
).toBe('這篇文章真的很厲害!')
expect(
truncateNoticeTitle('很厲害!', {
locale: UserLanguage.ZhHant,
maxLength: 10,
includeAtSign: true,
})
).toBe('很厲害!')
})

it('should truncate if over 10 characters', () => {
expect(
truncateNoticeTitle('這篇文章真的很厲害,大家應該都來看一下!', {
locale: UserLanguage.ZhHant,
maxLength: 10,
includeAtSign: true,
})
).toBe('這篇文章真的很厲害,...')
})

it('should truncate when the title is over 10 characters and the mentions are at the end', () => {
expect(
truncateNoticeTitle(
'這篇文章真的很厲害,大家應該都來看一下 @user1 @user2',
{ locale: UserLanguage.ZhHant, maxLength: 10, includeAtSign: true }
)
).toBe('這篇文章真的很厲害,...@user1 @user2')
expect(
truncateNoticeTitle(
'這篇文章真的很厲害,大家應該都來看一下! @user1 @user2',
{ locale: UserLanguage.ZhHant, maxLength: 10, includeAtSign: true }
)
).toBe('這篇文章真的很厲害,...@user1 @user2')
expect(
truncateNoticeTitle('這是一個時刻!!!!!!!@jj', {
locale: UserLanguage.ZhHant,
maxLength: 10,
includeAtSign: true,
})
).toBe('這是一個時刻!!!!...@jj')
})

it('should truncate if over 10 characters with tagged users in the middle or the beginning', () => {
expect(
truncateNoticeTitle('我和 @zhangsan 在台北一起去吃吃吃!', {
locale: UserLanguage.ZhHans,
maxLength: 10,
includeAtSign: true,
})
).toBe('我和 @zhangsan 在台北一起去...')
expect(
truncateNoticeTitle('@zhangsan 和我在台北一起去吃吃吃!', {
locale: UserLanguage.ZhHans,
maxLength: 10,
includeAtSign: true,
})
).toBe('@zhangsan 和我在台北一起去吃...')
})

it('should truncate characters to when the mention is a bit spread out', () => {
expect(
truncateNoticeTitle('我和 @zhangsan 還有 @yp 在台北一起去吃吃吃!', {
locale: UserLanguage.ZhHans,
maxLength: 10,
includeAtSign: true,
})
).toBe('我和 @zhangsan 還有 @yp 在台...')
})
})
})
Loading
Loading