1+ // src/utils/__tests__/priceChange.utils.test.ts
2+ import { describe , expect , it } from 'vitest' ;
3+ import { computePriceChange } from '../priceChange.utils' ;
4+
5+ describe ( 'computePriceChange' , ( ) => {
6+ describe ( 'price up' , ( ) => {
7+ it ( 'returns positive percent and direction up when price increases' , ( ) => {
8+ const result = computePriceChange ( 112n , 100n ) ;
9+ expect ( result . direction ) . toBe ( 'up' ) ;
10+ expect ( result . percent ) . toBeCloseTo ( 12 ) ;
11+ } ) ;
12+
13+ it ( 'handles a large price increase' , ( ) => {
14+ const result = computePriceChange ( 200n , 100n ) ;
15+ expect ( result . direction ) . toBe ( 'up' ) ;
16+ expect ( result . percent ) . toBeCloseTo ( 100 ) ;
17+ } ) ;
18+ } ) ;
19+
20+ describe ( 'price down' , ( ) => {
21+ it ( 'returns negative percent and direction down when price decreases' , ( ) => {
22+ const result = computePriceChange ( 88n , 100n ) ;
23+ expect ( result . direction ) . toBe ( 'down' ) ;
24+ expect ( result . percent ) . toBeCloseTo ( - 12 ) ;
25+ } ) ;
26+
27+ it ( 'handles a large price decrease' , ( ) => {
28+ const result = computePriceChange ( 1n , 100n ) ;
29+ expect ( result . direction ) . toBe ( 'down' ) ;
30+ expect ( result . percent ) . toBeCloseTo ( - 99 ) ;
31+ } ) ;
32+ } ) ;
33+
34+ describe ( 'flat — no change' , ( ) => {
35+ it ( 'returns flat with 0 percent when current equals previous' , ( ) => {
36+ const result = computePriceChange ( 100n , 100n ) ;
37+ expect ( result . direction ) . toBe ( 'flat' ) ;
38+ expect ( result . percent ) . toBe ( 0 ) ;
39+ } ) ;
40+ } ) ;
41+
42+ describe ( 'flat — zero previous value' , ( ) => {
43+ it ( 'returns flat with 0 percent when previous is zero' , ( ) => {
44+ const result = computePriceChange ( 100n , 0n ) ;
45+ expect ( result . direction ) . toBe ( 'flat' ) ;
46+ expect ( result . percent ) . toBe ( 0 ) ;
47+ } ) ;
48+
49+ it ( 'returns flat when both current and previous are zero' , ( ) => {
50+ const result = computePriceChange ( 0n , 0n ) ;
51+ expect ( result . direction ) . toBe ( 'flat' ) ;
52+ expect ( result . percent ) . toBe ( 0 ) ;
53+ } ) ;
54+ } ) ;
55+ } ) ;
0 commit comments