Skip to content

Commit eef3eac

Browse files
Removed invariant check from accessors
1 parent 27c28ff commit eef3eac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+129
-266
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "complex-js",
3-
"version": "7.1.0",
3+
"version": "7.1.1",
44
"description": "Complex math module for JavaScript",
55
"repository": "github:patrickroberts/complex",
66
"homepage": "https://patrickroberts.github.io/complex/",

src/accessors/__mocks__/abs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Complex from '../../complex';
22

3-
const abs = (_: typeof Complex, z: Complex): number => z._abs;
3+
const abs = (z: Complex): number => z._abs;
44

55
export default jest.fn(abs);

src/accessors/__mocks__/arg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Complex from '../../complex';
22

3-
const arg = (_: typeof Complex, z: Complex): number => z._arg;
3+
const arg = (z: Complex): number => z._arg;
44

55
export default jest.fn(arg);

src/accessors/__mocks__/imag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Complex from '../../complex';
22

3-
const imag = (_: typeof Complex, z: Complex): number => z._imag;
3+
const imag = (z: Complex): number => z._imag;
44

55
export default jest.fn(imag);

src/accessors/__mocks__/real.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Complex from '../../complex';
22

3-
const real = (_: typeof Complex, z: Complex): number => z._real;
3+
const real = (z: Complex): number => z._real;
44

55
export default jest.fn(real);

src/accessors/abs.test.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ import _ from '../__fixtures__/any/number';
22
import mock from '../__fixtures__/mock';
33

44
import Complex from '../complex';
5-
import { Component, invariant } from '../internal';
5+
import { Component } from '../internal';
66
import abs from '../math/abs';
77
import sut from './abs';
88

99
jest.mock('../complex');
10-
jest.mock('../internal/invariant');
1110
jest.mock('../math/abs');
1211

1312
beforeEach(() => {
14-
mock(invariant).mockClear();
1513
mock(abs).mockClear();
1614
});
1715

@@ -24,9 +22,8 @@ test.each<[Component]>([
2422
const expected = {} as number;
2523
const z = new Complex(_, _, expected, _, testHas);
2624

27-
const actual = sut(Complex, z);
25+
const actual = sut(z);
2826

29-
expect(invariant).toHaveBeenCalledWith(Complex, z);
3027
expect(abs).not.toHaveBeenCalled();
3128
expect(z._abs).toBe(expected);
3229
expect(z._has).toBe(testHas);
@@ -44,26 +41,10 @@ test.each<[Component]>([
4441

4542
mock(abs).mockReturnValueOnce(expected);
4643

47-
const actual = sut(Complex, z);
44+
const actual = sut(z);
4845

49-
expect(invariant).toHaveBeenCalledWith(Complex, z);
5046
expect(abs).toHaveBeenCalledWith(testReal, testImag);
5147
expect(z._abs).toBe(expected);
5248
expect(z._has).toBe(testHas | Component.ABS);
5349
expect(actual).toBe(expected);
5450
});
55-
56-
it('should not modify computed value if invariant is violated', () => {
57-
const z = {} as Complex;
58-
const expected = new TypeError();
59-
60-
mock(invariant).mockImplementationOnce(() => {
61-
throw expected;
62-
});
63-
64-
expect(() => sut(Complex, z)).toThrowError(expected);
65-
expect(invariant).toHaveBeenCalledWith(Complex, z);
66-
expect(abs).not.toHaveBeenCalled();
67-
expect(z).not.toHaveProperty('_abs');
68-
expect(z).not.toHaveProperty('_has');
69-
});

src/accessors/abs.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import Complex from '../complex';
2-
import { Component, invariant } from '../internal';
2+
import { Component } from '../internal';
33
import _abs from '../math/abs';
44

5-
const abs = (Ctor: typeof Complex, z: Complex): number => {
6-
invariant(Ctor, z);
7-
5+
const abs = (z: Complex): number => {
86
if (!(z._has & Component.ABS)) {
97
z._abs = _abs(z._real, z._imag);
108
z._has |= Component.ABS;

src/accessors/arg.test.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import _ from '../__fixtures__/any/number';
22
import mock from '../__fixtures__/mock';
33

44
import Complex from '../complex';
5-
import { Component, invariant } from '../internal';
5+
import { Component } from '../internal';
66
import arg from '../math/arg';
77
import sut from './arg';
88

99
jest.mock('../complex');
10-
jest.mock('../internal/invariant');
1110
jest.mock('../internal/principal');
1211
jest.mock('../math/arg');
1312

1413
beforeEach(() => {
15-
mock(invariant).mockClear();
1614
mock(arg).mockClear();
1715
});
1816

@@ -25,9 +23,8 @@ test.each<[Component]>([
2523
const expected = {} as number;
2624
const z = new Complex(_, _, _, expected, testHas);
2725

28-
const actual = sut(Complex, z);
26+
const actual = sut(z);
2927

30-
expect(invariant).toHaveBeenCalledWith(Complex, z);
3128
expect(arg).not.toHaveBeenCalled();
3229
expect(z._arg).toBe(expected);
3330
expect(z._has).toBe(testHas);
@@ -45,26 +42,10 @@ test.each<[Component]>([
4542

4643
mock(arg).mockReturnValueOnce(expected);
4744

48-
const actual = sut(Complex, z);
45+
const actual = sut(z);
4946

50-
expect(invariant).toHaveBeenCalledWith(Complex, z);
5147
expect(arg).toHaveBeenCalledWith(testReal, testImag);
5248
expect(z._arg).toBe(expected);
5349
expect(z._has).toBe(testHas | Component.ARG);
5450
expect(actual).toBe(expected);
5551
});
56-
57-
it('should not modify computed value if invariant is violated', () => {
58-
const z = {} as Complex;
59-
const expected = new TypeError();
60-
61-
mock(invariant).mockImplementationOnce(() => {
62-
throw expected;
63-
});
64-
65-
expect(() => sut(Complex, z)).toThrowError(expected);
66-
expect(invariant).toHaveBeenCalledWith(Complex, z);
67-
expect(arg).not.toHaveBeenCalled();
68-
expect(z).not.toHaveProperty('_arg');
69-
expect(z).not.toHaveProperty('_has');
70-
});

src/accessors/arg.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import Complex from '../complex';
2-
import { Component, invariant } from '../internal';
2+
import { Component } from '../internal';
33
import _arg from '../math/arg';
44

5-
const arg = (Ctor: typeof Complex, z: Complex): number => {
6-
invariant(Ctor, z);
7-
5+
const arg = (z: Complex): number => {
86
if (!(z._has & Component.ARG)) {
97
z._arg = _arg(z._real, z._imag);
108
z._has |= Component.ARG;

src/accessors/imag.test.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import _ from '../__fixtures__/any/number';
22
import mock from '../__fixtures__/mock';
33

44
import Complex from '../complex';
5-
import { Component, invariant } from '../internal';
5+
import { Component } from '../internal';
66
import imag from '../math/imag';
77
import sut from './imag';
88

99
jest.mock('../complex');
10-
jest.mock('../internal/invariant');
1110
jest.mock('../internal/principal');
1211
jest.mock('../math/imag');
1312

1413
beforeEach(() => {
15-
mock(invariant).mockClear();
1614
mock(imag).mockClear();
1715
});
1816

@@ -25,9 +23,8 @@ test.each<[Component]>([
2523
const expected = {} as number;
2624
const z = new Complex(_, expected, _, _, testHas);
2725

28-
const actual = sut(Complex, z);
26+
const actual = sut(z);
2927

30-
expect(invariant).toHaveBeenCalledWith(Complex, z);
3128
expect(imag).not.toHaveBeenCalled();
3229
expect(z._imag).toBe(expected);
3330
expect(z._has).toBe(testHas);
@@ -45,26 +42,10 @@ test.each<[Component]>([
4542

4643
mock(imag).mockReturnValueOnce(expected);
4744

48-
const actual = sut(Complex, z);
45+
const actual = sut(z);
4946

50-
expect(invariant).toHaveBeenCalledWith(Complex, z);
5147
expect(imag).toHaveBeenCalledWith(testAbs, testArg);
5248
expect(z._imag).toBe(expected);
5349
expect(z._has).toBe(testHas | Component.IMAG);
5450
expect(actual).toBe(expected);
5551
});
56-
57-
it('should not modify computed value if invariant is violated', () => {
58-
const z = {} as Complex;
59-
const expected = new TypeError();
60-
61-
mock(invariant).mockImplementationOnce(() => {
62-
throw expected;
63-
});
64-
65-
expect(() => sut(Complex, z)).toThrowError(expected);
66-
expect(invariant).toHaveBeenCalledWith(Complex, z);
67-
expect(imag).not.toHaveBeenCalled();
68-
expect(z).not.toHaveProperty('_imag');
69-
expect(z).not.toHaveProperty('_has');
70-
});

0 commit comments

Comments
 (0)