Skip to content

Commit

Permalink
fix(tests): remove unnecessary setups in describes
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Dec 12, 2023
1 parent b5b5036 commit e4cbab8
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 124 deletions.
4 changes: 2 additions & 2 deletions src/actions/read/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ describe('balance', () => {
it.each(['test', '?', false, Infinity, 0, -1, parseInt(''.padEnd(43, '1'))])(
'should throw error on invalid address type',
async (user: any) => {
const _state = { ...state, balances: { [user]: 1 } };
const result = await balance(_state, {
const initState = { ...state, balances: { [user]: 1 } };
const result = await balance(initState, {
caller: 'test',
input: {
function: 'balance',
Expand Down
39 changes: 25 additions & 14 deletions src/actions/write/evolve.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,31 @@ describe('evolve', () => {
expect(error.message).toEqual(INVALID_INPUT_MESSAGE);
});

it.each(['hacker', undefined, null, false, true, Infinity, 1, 0, -1, [], {}])(
'should throw an error on bad caller',
async (badCaller: string) => {
const error: any = await evolve(baselineAntState, {
caller: badCaller,
input: {
function: 'evolve',
value: '1111111111111111111111111111111111111111111',
},
}).catch((e) => e);
expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual(NON_CONTRACT_OWNER_MESSAGE);
},
);
it.each([
'hacker',
undefined,
null,
false,
true,
Infinity,
1,
0,
-1,
[],
{},
''.padEnd(44, '1'),
'?'.padEnd(42, '1'),
])('should throw an error on bad caller', async (badCaller: string) => {
const error: any = await evolve(baselineAntState, {
caller: badCaller,
input: {
function: 'evolve',
value: '1111111111111111111111111111111111111111111',
},
}).catch((e) => e);
expect(error).toBeInstanceOf(Error);
expect(error.message).toEqual(NON_CONTRACT_OWNER_MESSAGE);
});

it.each([
{
Expand Down
24 changes: 9 additions & 15 deletions src/actions/write/removeController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ import {
import { removeController } from './removeController';

describe('removeController', () => {
let state = { ...baselineAntState };

beforeEach(() => {
state = { ...baselineAntState };
});

it.each([
undefined,
false,
Expand All @@ -40,8 +34,8 @@ describe('removeController', () => {
'?'.padEnd(42, '1'),
''.padEnd(44, '1'),
])('should throw on bad target', async (target: any) => {
const _state = { ...state, controllers: [target] };
const result = await removeController(_state, {
const initState = { ...baselineAntState, controllers: [target] };
const result = await removeController(initState, {
caller: 'test',
input: {
function: 'removeController',
Expand All @@ -54,8 +48,8 @@ describe('removeController', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should not remove controller as non-owner',
async (target: string) => {
const _state = { ...state, controllers: [target] };
const result = await removeController(_state, {
const initState = { ...baselineAntState, controllers: [target] };
const result = await removeController(initState, {
caller: target,
input: {
function: 'removeController',
Expand All @@ -69,8 +63,8 @@ describe('removeController', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should throw if target not a controller',
async (target: string) => {
const _state = { ...state, controllers: [] };
const result = await removeController(_state, {
const initState = { ...baselineAntState, controllers: [] };
const result = await removeController(initState, {
caller: 'test',
input: {
function: 'removeController',
Expand All @@ -86,16 +80,16 @@ describe('removeController', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should remove controller',
async (target: string) => {
const _state = { ...state, controllers: [target] };
const result = (await removeController(_state, {
const initState = { ...baselineAntState, controllers: [target] };
const result = (await removeController(initState, {
caller: 'test',
input: {
function: 'removeController',
target,
},
})) as AntContractWriteResult;

expect(result.state).not.toContain(target);
expect(result.state.controllers).not.toContain(target);
},
);
});
34 changes: 14 additions & 20 deletions src/actions/write/removeRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ import {
import { removeRecord } from './removeRecord';

describe('removeRecord', () => {
let state = { ...baselineAntState };

beforeEach(() => {
state = { ...baselineAntState };
});

it.each([
undefined,
false,
Expand All @@ -45,16 +39,16 @@ describe('removeRecord', () => {
'=',
'?'.padEnd(MAX_NAME_LENGTH + 1, '1'),
])('should throw on bad domain', async (subDomain: any) => {
const _state = {
...state,
const initState = {
...baselineAntState,
records: {
[subDomain]: {
transactionId: ''.padEnd(43, '1'),
ttlSeconds: MIN_TTL_LENGTH,
},
},
};
const result = await removeRecord(_state, {
const result = await removeRecord(initState, {
caller: 'test',
input: {
function: 'removeRecord',
Expand All @@ -67,16 +61,16 @@ describe('removeRecord', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should not remove domain as non-owner or controller',
async (hacker: string) => {
const _state = {
...state,
const initState = {
...baselineAntState,
records: {
subDomain: {
transactionId: ''.padEnd(43, '1'),
ttlSeconds: MIN_TTL_LENGTH,
},
},
};
const result = await removeRecord(_state, {
const result = await removeRecord(initState, {
caller: hacker,
input: {
function: 'removeRecord',
Expand All @@ -90,8 +84,8 @@ describe('removeRecord', () => {
it.each(['fibonachi', 'sequence1', 'tetra-hedron'])(
'should throw if domain not a record',
async (subDomain: string) => {
const _state = { ...state, controllers: [] };
const result = await removeRecord(_state, {
const initState = { ...baselineAntState, controllers: [] };
const result = await removeRecord(initState, {
caller: 'test',
input: {
function: 'removeRecord',
Expand All @@ -106,8 +100,8 @@ describe('removeRecord', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should remove domain as owner',
async (owner: string) => {
const _state = {
...state,
const initState = {
...baselineAntState,
owner,
records: {
subDomain: {
Expand All @@ -116,7 +110,7 @@ describe('removeRecord', () => {
},
},
};
const result = (await removeRecord(_state, {
const result = (await removeRecord(initState, {
caller: owner,
input: {
function: 'removeRecord',
Expand All @@ -130,8 +124,8 @@ describe('removeRecord', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should remove domain as controller',
async (controller: string) => {
const _state = {
...state,
const initState = {
...baselineAntState,
controllers: [controller],
records: {
subDomain: {
Expand All @@ -140,7 +134,7 @@ describe('removeRecord', () => {
},
},
};
const result = (await removeRecord(_state, {
const result = (await removeRecord(initState, {
caller: controller,
input: {
function: 'removeRecord',
Expand Down
22 changes: 8 additions & 14 deletions src/actions/write/setController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ import { baselineAntState } from '../../../tests/utils/constants';
import { setController } from './setController';

describe('setController', () => {
let state = { ...baselineAntState };

beforeEach(() => {
state = { ...baselineAntState };
});

it.each([
undefined,
false,
Expand All @@ -39,8 +33,8 @@ describe('setController', () => {
'?'.padEnd(42, '1'),
''.padEnd(44, '2'),
])('should throw on bad target', async (target: any) => {
const _state = { ...state, controllers: [] };
const result = await setController(_state, {
const initState = { ...baselineAntState, controllers: [] };
const result = await setController(initState, {
caller: 'test',
input: {
function: 'setController',
Expand All @@ -53,8 +47,8 @@ describe('setController', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should not set controller if already set',
async (target: string) => {
const _state = { ...state, controllers: [target] };
const result = await setController(_state, {
const initState = { ...baselineAntState, controllers: [target] };
const result = await setController(initState, {
caller: 'test',
input: {
function: 'setController',
Expand All @@ -69,8 +63,8 @@ describe('setController', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should not set controller as non-owner',
async (target: string) => {
const _state = { ...state, controllers: [] };
const result = await setController(_state, {
const initState = { ...baselineAntState, controllers: [] };
const result = await setController(initState, {
caller: target,
input: {
function: 'setController',
Expand All @@ -84,8 +78,8 @@ describe('setController', () => {
it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])(
'should set controller',
async (target: string) => {
const _state = { ...state, controllers: [] };
const result = (await setController(_state, {
const initState = { ...baselineAntState, controllers: [] };
const result = (await setController(initState, {
caller: 'test',
input: {
function: 'setController',
Expand Down
19 changes: 8 additions & 11 deletions src/actions/write/setName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@ import { baselineAntState } from '../../../tests/utils/constants';
import { setName } from './setName';

describe('setName', () => {
let state = { ...baselineAntState };

beforeEach(() => {
state = { ...baselineAntState };
});

it('should throw if not owner or controller', async () => {
const result = await setName(state, {
const result = await setName(baselineAntState, {
caller: ''.padEnd(43, '1'),
input: {
function: 'setName',
Expand All @@ -43,7 +37,7 @@ describe('setName', () => {
it.each([undefined, Infinity, 1, 0, -1, 1.1, true, false, null, {}, []])(
'should throw on bad name',
async (name: any) => {
const result = await setName(state, {
const result = await setName(baselineAntState, {
caller: 'test',
input: {
function: 'setName',
Expand All @@ -55,7 +49,7 @@ describe('setName', () => {
);

it('should set name as owner', async () => {
const result = (await setName(state, {
const result = (await setName(baselineAntState, {
caller: 'test',
input: {
function: 'setName',
Expand All @@ -66,8 +60,11 @@ describe('setName', () => {
});

it('should set name as controller', async () => {
const _state = { ...state, controllers: [''.padEnd(43, '1')] };
const result = (await setName(_state, {
const initState = {
...baselineAntState,
controllers: [''.padEnd(43, '1')],
};
const result = (await setName(initState, {
caller: ''.padEnd(43, '1'),
input: {
function: 'setName',
Expand Down
Loading

0 comments on commit e4cbab8

Please sign in to comment.