mattermost-mobile-upstream/app/components/animated_number/index.test.tsx
Elias Nahum b6fc85385e
Migrate from RNN to Expo router (#9402)
* Expo-Router partial migration

* Full migration to ExpoRouter

* i18n

* add tests

* Merge branch 'main' into expo-router

* align package-lock.json

* i18n

* feedback review

* feedback review

* android 16kb patch

* Refactored emoji picker keyboard using a state machine

* set android launch_screen in drawables instead of layout

* no conditional hooks

* remove duplicate call in test

* address feedback

* fix tests and address more feedback

* small corrections

* small corrections

* small corrections

* fix draft test

* fix draft test 2

* remove navigateToScreen in draff test

* fix nit in draft test

* remove doc file

* Feedback review

* fix snapshots

* improve test coverage

* fix i18n

* Upgrade t o RN 0.83, other dependencies and new architecture (#9737)

* library hardware-keyboard android new arch support fix

* library rnshare android new arch support fix

* library rnutils android new arch support fix

* library secure-pdf-viewer add new arch support

* Enable new arch on iOS and Android

* update emm and network client with new-arch

* update intune to use new-arch

* performance improvements

* fix tests

* fixes post input on new-arch

* fix android emoji picker

* remove doc

* run tests in parallel

* upgrade to React Native 0.83.9, Expo 55, React 19, and New Architecture

Bumps core dependencies to their latest versions:
- React Native 0.77.3 → 0.83.9 (New Architecture enabled)
- React 18.3.1 → 19.2.5
- Expo SDK 52 → 55
- TypeScript 5.8.3 → 6.0.3
- Node.js 22.16.0 → 24.15.0
- react-native-reanimated 3.17.3 → 4.3.0
- Various other dependency upgrades

Migrates iOS AppDelegate from Objective-C to Swift, updates Android
build tooling and Gradle wrapper, refreshes all patches and font
assets, and adapts app code, tests, and CI workflows throughout for
API changes introduced by these upgrades.

* remove unecessary dismissKeyboard in notifications

* feedback review

* update intune to use new-arch

* update @mattermost deps

* missing i18n string

* additional fixes

* update fastlane
2026-05-13 17:44:55 +08:00

134 lines
5.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {fireEvent, render, waitFor, screen} from '@testing-library/react-native';
import React from 'react';
import {Animated} from 'react-native';
import AnimatedNumber from './index';
const NUMBER_HEIGHT = 10;
describe('AnimatedNumber', () => {
// running on jest, since Animated is a native module, Animated.timing.start needs to be mocked in order to update to the final Animated.Value.
// Ex: 1 => 2, the Animated.Value should be -20 (from -10) after the animation is done
jest.spyOn(Animated, 'timing').mockImplementation((a, b) => ({
// @ts-expect-error mock implementation for testing
start: jest.fn().mockImplementation(() => a.setValue(b.toValue)),
}) as unknown as Animated.CompositeAnimation);
it('should render the non-animated number', () => {
render(<AnimatedNumber animateToNumber={123}/>);
const text = screen.getByTestId('no-animation-number');
expect(text.children).toContainEqual('123');
});
it('should removed the non-animation number after getting the correct height', () => {
render(<AnimatedNumber animateToNumber={123}/>);
const text = screen.getByTestId('no-animation-number');
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT}}});
const removedText = screen.queryByTestId('no-animation-number');
expect(removedText).toBeNull();
});
it('should switch to the animated number view', async () => {
render(<AnimatedNumber animateToNumber={123}/>);
const text = screen.getByTestId('no-animation-number');
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT}}});
const animatedView = screen.getByTestId('animation-number-main');
expect(animatedView).toBeTruthy();
});
describe.each([1, 23, 579, -123, 6789, 23456])('should show the correct number of animated views based on the digits', (animateToNumber: number) => {
const numberOfDigits = animateToNumber.toString().length;
it(`should display ${numberOfDigits} view(s) for ${animateToNumber}`, async () => {
render(<AnimatedNumber animateToNumber={animateToNumber}/>);
const text = screen.getByTestId('no-animation-number');
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT}}});
await waitFor(() => {
const animatedView = screen.getByTestId('animation-number-main');
expect(animatedView.children).toHaveLength(numberOfDigits);
});
});
});
describe.each([123, 9982, 12345, 901876, -157])('should show the correct number', (animateToNumber: number) => {
const absAnimatedNumberString = String(Math.abs(animateToNumber));
const numberOfDigits = absAnimatedNumberString.length;
it(`should display the number ${animateToNumber}`, async () => {
render(<AnimatedNumber animateToNumber={animateToNumber}/>);
const text = screen.getByTestId('no-animation-number');
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT}}});
const checkEachDigit = absAnimatedNumberString.split('').map(async (number, index) => {
const useIndex = numberOfDigits - 1 - index;
// every digit will have a row of 10 numbers, so the translateY should be the height of the number * the number * -1 (since the animation is going up)
const transformedView = screen.getByTestId(`animated-number-view-${useIndex}`);
const {translateY} = transformedView.props.style.transform[0];
expect(Math.abs(translateY / NUMBER_HEIGHT)).toEqual(Number(number));
});
await Promise.all(checkEachDigit);
});
});
describe.each([146, 144, 1, 1000000, -145])('should rerender the correct number that it animates to', (animateToNumber: number) => {
it(`should display the number ${animateToNumber}`, async () => {
const startingNumber = 145;
render(<AnimatedNumber animateToNumber={startingNumber}/>);
const text = screen.getByTestId('no-animation-number');
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT}}});
screen.rerender(<AnimatedNumber animateToNumber={animateToNumber}/>);
const animateToNumberString = String(Math.abs(animateToNumber));
const checkEachDigit = animateToNumberString.split('').map(async (number, index) => {
const useIndex = animateToNumberString.length - 1 - index;
const transformedView = screen.getByTestId(`animated-number-view-${useIndex}`);
const {translateY} = transformedView.props.style.transform[0];
expect(Math.abs((translateY) / NUMBER_HEIGHT)).toEqual(Number(number));
});
await Promise.all(checkEachDigit);
});
});
it('KNOWN UI BUG: should show that there will be an issue if the text height changes, due to the non-animated number view has been removed', async () => {
// the number text will get cut-off if the user changes the text size on their mobile devices
render(<AnimatedNumber animateToNumber={123}/>);
const text = screen.getByTestId('no-animation-number');
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT}}});
try {
fireEvent(text, 'onLayout', {nativeEvent: {layout: {height: NUMBER_HEIGHT + NUMBER_HEIGHT}}});
} catch (e) {
expect(e).toEqual(new Error('Unable to find node on an unmounted component.'));
}
const animatedView = screen.getByTestId('animation-number-main');
expect(animatedView).toBeTruthy();
});
});