-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #538 from systemli/Remove-old-Twitter-Message-Limit
🔥 Remove old Twitter Message Limit
- Loading branch information
Showing
3 changed files
with
52 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import MessageFormCounter from './MessageFormCounter' | ||
|
||
describe('MessageFormCounter', () => { | ||
it('should render the correct letter count', () => { | ||
const letterCount = 10 | ||
const maxLength = 100 | ||
render(<MessageFormCounter letterCount={letterCount} maxLength={maxLength} />) | ||
const letterCountElement = screen.getByText(`${letterCount}/${maxLength}`) | ||
expect(letterCountElement.parentElement).toHaveClass('MuiChip-colorSuccess') | ||
}) | ||
|
||
it('should render a red chip when the letter count is over the limit', () => { | ||
const letterCount = 110 | ||
const maxLength = 100 | ||
render(<MessageFormCounter letterCount={letterCount} maxLength={maxLength} />) | ||
const letterCountElement = screen.getByText(`${letterCount}/${maxLength}`) | ||
expect(letterCountElement.parentElement).toHaveClass('MuiChip-colorError') | ||
}) | ||
|
||
it('should render a green chip when the letter count is under the limit', () => { | ||
const letterCount = 90 | ||
const maxLength = 100 | ||
render(<MessageFormCounter letterCount={letterCount} maxLength={maxLength} />) | ||
const letterCountElement = screen.getByText(`${letterCount}/${maxLength}`) | ||
expect(letterCountElement.parentElement).toHaveClass('MuiChip-colorWarning') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
import { ChipPropsColorOverrides, Chip } from '@mui/material' | ||
import { OverridableStringUnion } from '@mui/types' | ||
import React, { FC, useEffect, useState } from 'react' | ||
import { MESSAGE_LIMIT } from './MessageForm' | ||
|
||
interface Props { | ||
maxLength: number | ||
letterCount: number | ||
} | ||
|
||
const MessageFormCounter: FC<Props> = ({ letterCount }) => { | ||
const MessageFormCounter: FC<Props> = ({ letterCount, maxLength }) => { | ||
const [color, setColor] = | ||
useState<OverridableStringUnion<'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning', ChipPropsColorOverrides>>('default') | ||
|
||
useEffect(() => { | ||
if (letterCount > MESSAGE_LIMIT) { | ||
if (letterCount > maxLength) { | ||
setColor('error') | ||
} else if (letterCount >= 240) { | ||
} else if (letterCount >= maxLength * 0.9) { | ||
setColor('warning') | ||
} else if (letterCount === 0) { | ||
setColor('default') | ||
} else { | ||
setColor('success') | ||
} | ||
}, [letterCount]) | ||
}, [letterCount, maxLength]) | ||
|
||
return <Chip color={color} label={`${letterCount}/${MESSAGE_LIMIT}`} size="small" variant="outlined" /> | ||
return <Chip color={color} label={`${letterCount}/${maxLength}`} size="small" variant="outlined" /> | ||
} | ||
|
||
export default MessageFormCounter |