Skip to content

test(bezier-react): Improved array test coverage #1775

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

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { MoreIcon } from '@channel.io/bezier-icons'

import { Typography } from '~/src/foundation'

import { isLastIndex } from '~/src/utils/array'
import { isLastArrayIndex } from '~/src/utils/array'
import { noop } from '~/src/utils/function'
import {
cssVarName,
Expand Down Expand Up @@ -135,7 +135,7 @@ forwardedRef: React.Ref<HTMLDivElement>,

const AvatarElement = renderAvatarElement(avatar, slicedAvatarList.length)

if (!isLastIndex(arr, index)) {
if (!isLastArrayIndex(arr, index)) {
return AvatarElement
}

Expand Down
39 changes: 28 additions & 11 deletions packages/bezier-react/src/utils/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
import {
compact,
flattenDeep,
isLastArrayIndex,
} from './array'

describe('arrayUtils', () => {
describe('compact', () => {
it('should remove falsy item', () => {
const array = [0, 1, false, 2, '', 3]
expect(compact(array)).toEqual([1, 2, 3])
})
describe('isLastArrayIndex', () => {
it('should return true when the last array index', () => {
const arr = [0, 1, 2, 3, 4]

const result = isLastArrayIndex(arr, 4)

expect(result).toBe(true)
})

it('should return false when not the last array index', () => {
const arr = [0, 1, 2, 3, 4]

const result = isLastArrayIndex(arr, 1)

expect(result).toBe(false)
})
})

describe('compact', () => {
it('should remove falsy item', () => {
const array = [0, 1, false, 2, '', 3]
expect(compact(array)).toEqual([1, 2, 3])
})
})

describe('flattenDeep', () => {
it('should return flatten array', () => {
const array = [1, [2, [3, [4]], 5]]
expect(flattenDeep(array)).toEqual([1, 2, 3, 4, 5])
})
describe('flattenDeep', () => {
it('should return flatten array', () => {
const array = [1, [2, [3, [4]], 5]]
expect(flattenDeep(array)).toEqual([1, 2, 3, 4, 5])
})
})
2 changes: 1 addition & 1 deletion packages/bezier-react/src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isArray } from './type'

export function isLastIndex(array: any[], index: number) {
export function isLastArrayIndex(array: any[], index: number) {
return array.length - 1 === index
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export function isLastIndex(array: ArrayLike<unknown>, index: number) {
  return array.length - 1 === index
}

이 PR의 범위는 아니지만, 지금보니 이 타입이 더 정확하겠네요 😅 유사배열도 length 프로퍼티를 가지고 있으니까요. 이렇게 변경할 경우 isLastIndex 라는 이름이 더 적절하지 않을까 생각이 드는데, 의견이 궁금합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 유사 배열까지는 생각을 못 했던 것 같아요! 그러면 isLastIndex가 맞을 것 같아요 좋은 의견 감사합니다~

Copy link
Contributor Author

@SEOKKAMONI SEOKKAMONI Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네이밍 롤백 타입 변경 두개의 커밋으로 나눠서 반영하였습니다~


Expand Down