Skip to content

Commit

Permalink
Update bn.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
VolodymyrBg authored Jan 17, 2025
1 parent aec0e50 commit dd386f8
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions packages/math/src/bn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,26 +542,34 @@ describe('Math - BN', () => {
expect(bn('4000000').format({ precision: 1 })).toEqual('0.004');
});

it('should warn when precision is less than minPrecision', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();

// Test case where precision < minPrecision
bn(123).format({ precision: 2, minPrecision: 4 });

expect(consoleWarnSpy).toHaveBeenCalledWith(
'Warning: precision (2) is less than minPrecision (4). ' +
'This may lead to unexpected behavior. Consider setting precision >= minPrecision.'
);

// Test case where precision = minPrecision (should not warn)
consoleWarnSpy.mockClear();
bn(123).format({ precision: 4, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();

// Test case where precision > minPrecision (should not warn)
bn(123).format({ precision: 6, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();

consoleWarnSpy.mockRestore();
describe('precision validation', () => {
let consoleWarnSpy: jest.SpyInstance;

beforeEach(() => {
consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
});

afterEach(() => {
consoleWarnSpy.mockRestore();
});

it('should warn when precision is less than minPrecision', () => {
bn(123).format({ precision: 2, minPrecision: 4 });

expect(consoleWarnSpy).toHaveBeenCalledWith(
'Warning: precision (2) is less than minPrecision (4). ' +
'This may lead to unexpected behavior. Consider setting precision >= minPrecision.'
);
});

it('should not warn when precision equals minPrecision', () => {
bn(123).format({ precision: 4, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();
});

it('should not warn when precision is greater than minPrecision', () => {
bn(123).format({ precision: 6, minPrecision: 4 });
expect(consoleWarnSpy).not.toHaveBeenCalled();
});
});
});

0 comments on commit dd386f8

Please sign in to comment.