import { createIntl } from 'react-intl' import { getLimit } from './limit-counter' describe('getLimit', () => { describe('with deprecated maxLength', () => { it('should default to truncate on, but no counter', () => { expect(getLimit(100, { maxLength: 105 })).toEqual({ limit: 105, showCounter: false, truncate: true, count: '', overCount: false, nearCount: false, }) }) }) describe('with deprecated maxLength and showCounter', () => { it('should default to truncate on and showCounter regardless of length', () => { expect(getLimit(40, { maxLength: 105, showCounter: true })).toEqual( { limit: 105, showCounter: true, truncate: true, count: '40/105', overCount: false, nearCount: false, } ) }) }) describe('with limit', () => { describe('with defaults', () => { it('should not show counter when not near the end', () => { expect(getLimit(40, { limit: 100 })).toEqual({ limit: 100, showCounter: false, truncate: true, count: '', overCount: false, nearCount: false, }) }) it('should show a counter when near the end', () => { expect(getLimit(85, { limit: 100 })).toEqual({ limit: 100, showCounter: true, truncate: true, count: '85/100', overCount: false, nearCount: false, }) }) it('should show a warning counter when very near the end', () => { expect(getLimit(95, { limit: 100 })).toEqual({ limit: 100, showCounter: true, truncate: true, count: '95/100', overCount: false, nearCount: true, }) }) it('should show a error counter when very over the end', () => { expect(getLimit(105, { limit: 100 })).toEqual({ limit: 100, showCounter: true, truncate: true, count: '105/100', overCount: true, nearCount: true, }) }) }) describe('with very small limit', () => { it('should not show counter when nothing is typed', () => { expect(getLimit(0, { limit: 10 })).toEqual({ limit: 10, showCounter: false, truncate: true, count: '', overCount: false, nearCount: false, }) }) it('should show counter when one character is typed', () => { expect(getLimit(1, { limit: 10 })).toEqual({ limit: 10, showCounter: true, truncate: true, count: '1/10', overCount: false, nearCount: false, }) }) it('should show warning a minimum of 2 characters from end', () => { expect(getLimit(4, { limit: 5 })).toEqual({ limit: 5, showCounter: true, truncate: true, count: '4/5', overCount: false, nearCount: true, }) }) }) describe('with very large limit', () => { it('should not show counter if not within final 500 characters', () => { expect(getLimit(2499, { limit: 3000 })).toEqual({ limit: 3000, showCounter: false, truncate: true, count: '', overCount: false, nearCount: false, }) }) it('should show counter when within final 500 characters', () => { expect(getLimit(2799, { limit: 3000 })).toEqual({ limit: 3000, showCounter: true, truncate: true, count: '2799/3000', overCount: false, nearCount: false, }) }) it('should show warning when within final 200 characters', () => { expect(getLimit(2801, { limit: 3000 })).toEqual({ limit: 3000, showCounter: true, truncate: true, count: '2801/3000', overCount: false, nearCount: true, }) }) it('should use intl to format numbers when present', () => { expect( getLimit( 2990, { limit: 3000 }, createIntl({ locale: 'en-US' }) ) ).toEqual({ limit: 3000, showCounter: true, truncate: true, count: '2,990/3,000', overCount: false, nearCount: true, }) }) }) describe('when always showing counter', () => { it('should honor the override', () => { expect( getLimit(20, { limit: { value: 100, show: 'always' } }) ).toEqual({ limit: 100, showCounter: true, truncate: true, count: '20/100', overCount: false, nearCount: false, }) }) }) describe('when not truncating', () => { it('should show a error counter when very over the end', () => { expect( getLimit(105, { limit: { value: 100, truncate: false } }) ).toEqual({ limit: 100, showCounter: true, truncate: false, count: '105/100', overCount: true, nearCount: true, }) }) }) }) describe('with deprecated maxLength and showCounter with new limit', () => { it('should ignore showCounter and maxLength when limit is defined', () => { expect( getLimit(40, { maxLength: 105, showCounter: true, limit: 100 }) ).toEqual({ limit: 100, showCounter: false, truncate: true, count: '', overCount: false, nearCount: false, }) }) }) })