From e4cbab846cb1ea48bf74d4f2dd97934481beba72 Mon Sep 17 00:00:00 2001 From: atticusofsparta Date: Tue, 12 Dec 2023 08:22:24 -0600 Subject: [PATCH] fix(tests): remove unnecessary setups in describes --- src/actions/read/balance.test.ts | 4 +-- src/actions/write/evolve.test.ts | 39 ++++++++++++++-------- src/actions/write/removeController.test.ts | 24 +++++-------- src/actions/write/removeRecord.test.ts | 34 ++++++++----------- src/actions/write/setController.test.ts | 22 +++++------- src/actions/write/setName.test.ts | 19 +++++------ src/actions/write/setRecord.test.ts | 26 ++++++--------- src/actions/write/setTicker.test.ts | 19 +++++------ src/actions/write/transfer.test.ts | 39 ++++++++++------------ 9 files changed, 102 insertions(+), 124 deletions(-) diff --git a/src/actions/read/balance.test.ts b/src/actions/read/balance.test.ts index e36e889..c2f75b3 100644 --- a/src/actions/read/balance.test.ts +++ b/src/actions/read/balance.test.ts @@ -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', diff --git a/src/actions/write/evolve.test.ts b/src/actions/write/evolve.test.ts index 4838638..20ac5b5 100644 --- a/src/actions/write/evolve.test.ts +++ b/src/actions/write/evolve.test.ts @@ -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([ { diff --git a/src/actions/write/removeController.test.ts b/src/actions/write/removeController.test.ts index 0ad10d2..b6d5178 100644 --- a/src/actions/write/removeController.test.ts +++ b/src/actions/write/removeController.test.ts @@ -24,12 +24,6 @@ import { import { removeController } from './removeController'; describe('removeController', () => { - let state = { ...baselineAntState }; - - beforeEach(() => { - state = { ...baselineAntState }; - }); - it.each([ undefined, false, @@ -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', @@ -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', @@ -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', @@ -86,8 +80,8 @@ 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', @@ -95,7 +89,7 @@ describe('removeController', () => { }, })) as AntContractWriteResult; - expect(result.state).not.toContain(target); + expect(result.state.controllers).not.toContain(target); }, ); }); diff --git a/src/actions/write/removeRecord.test.ts b/src/actions/write/removeRecord.test.ts index f0d3821..1e09518 100644 --- a/src/actions/write/removeRecord.test.ts +++ b/src/actions/write/removeRecord.test.ts @@ -27,12 +27,6 @@ import { import { removeRecord } from './removeRecord'; describe('removeRecord', () => { - let state = { ...baselineAntState }; - - beforeEach(() => { - state = { ...baselineAntState }; - }); - it.each([ undefined, false, @@ -45,8 +39,8 @@ 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'), @@ -54,7 +48,7 @@ describe('removeRecord', () => { }, }, }; - const result = await removeRecord(_state, { + const result = await removeRecord(initState, { caller: 'test', input: { function: 'removeRecord', @@ -67,8 +61,8 @@ 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'), @@ -76,7 +70,7 @@ describe('removeRecord', () => { }, }, }; - const result = await removeRecord(_state, { + const result = await removeRecord(initState, { caller: hacker, input: { function: 'removeRecord', @@ -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', @@ -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: { @@ -116,7 +110,7 @@ describe('removeRecord', () => { }, }, }; - const result = (await removeRecord(_state, { + const result = (await removeRecord(initState, { caller: owner, input: { function: 'removeRecord', @@ -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: { @@ -140,7 +134,7 @@ describe('removeRecord', () => { }, }, }; - const result = (await removeRecord(_state, { + const result = (await removeRecord(initState, { caller: controller, input: { function: 'removeRecord', diff --git a/src/actions/write/setController.test.ts b/src/actions/write/setController.test.ts index 736a488..a928244 100644 --- a/src/actions/write/setController.test.ts +++ b/src/actions/write/setController.test.ts @@ -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, @@ -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', @@ -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', @@ -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', @@ -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', diff --git a/src/actions/write/setName.test.ts b/src/actions/write/setName.test.ts index 4b563d5..56eb3cb 100644 --- a/src/actions/write/setName.test.ts +++ b/src/actions/write/setName.test.ts @@ -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', @@ -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', @@ -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', @@ -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', diff --git a/src/actions/write/setRecord.test.ts b/src/actions/write/setRecord.test.ts index 1912bb9..ac444df 100644 --- a/src/actions/write/setRecord.test.ts +++ b/src/actions/write/setRecord.test.ts @@ -25,16 +25,10 @@ import { baselineAntState } from '../../../tests/utils/constants'; import { setRecord } from './setRecord'; describe('setRecord', () => { - let state = { ...baselineAntState }; - - beforeEach(() => { - state = { ...baselineAntState }; - }); - it.each([''.padEnd(43, '_'), ''.padEnd(43, 'a'), ''.padEnd(43, '1')])( 'should throw if not owner or controller', async (caller) => { - const result = await setRecord(state, { + const result = await setRecord(baselineAntState, { caller, input: { function: 'setRecord', @@ -59,7 +53,7 @@ describe('setRecord', () => { '=', '?'.padEnd(MAX_NAME_LENGTH + 1, '1'), ])('should throw on bad subDomain', async (subDomain: any) => { - const result = await setRecord(state, { + const result = await setRecord(baselineAntState, { caller: 'test', input: { function: 'setRecord', @@ -74,7 +68,7 @@ describe('setRecord', () => { it.each(['www'])( 'should throw on invalid or reserved subDomain', async (subDomain: any) => { - const result = await setRecord(state, { + const result = await setRecord(baselineAntState, { caller: 'test', input: { function: 'setRecord', @@ -99,7 +93,7 @@ describe('setRecord', () => { '=', '?'.padEnd(MAX_NAME_LENGTH + 1, '1'), ])('should throw on bad transactionId', async (transactionId: any) => { - const result = await setRecord(state, { + const result = await setRecord(baselineAntState, { caller: 'test', input: { function: 'setRecord', @@ -123,7 +117,7 @@ describe('setRecord', () => { '=', '?'.padEnd(MAX_NAME_LENGTH + 1, '1'), ])('should throw on bad ttlSeconds', async (ttlSeconds: any) => { - const result = await setRecord(state, { + const result = await setRecord(baselineAntState, { caller: 'test', input: { function: 'setRecord', @@ -138,11 +132,11 @@ describe('setRecord', () => { it.each([''.padEnd(43, '1'), ''.padEnd(43, 'a')])( 'should set record as controller', async (controller: string) => { - const _state = { - ...state, + const initState = { + ...baselineAntState, controllers: [controller], }; - const result = (await setRecord(_state, { + const result = (await setRecord(initState, { caller: controller, input: { function: 'setRecord', @@ -161,8 +155,8 @@ describe('setRecord', () => { ); it('should set record as owner', async () => { - const result = (await setRecord(state, { - caller: state.owner, + const result = (await setRecord(baselineAntState, { + caller: baselineAntState.owner, input: { function: 'setRecord', subDomain: 'domain', diff --git a/src/actions/write/setTicker.test.ts b/src/actions/write/setTicker.test.ts index 2cc58e0..31105c4 100644 --- a/src/actions/write/setTicker.test.ts +++ b/src/actions/write/setTicker.test.ts @@ -23,14 +23,8 @@ import { baselineAntState } from '../../../tests/utils/constants'; import { setTicker } from './setTicker'; describe('setTicker', () => { - let state = { ...baselineAntState }; - - beforeEach(() => { - state = { ...baselineAntState }; - }); - it('should throw if not owner or controller', async () => { - const result = await setTicker(state, { + const result = await setTicker(baselineAntState, { caller: ''.padEnd(43, '1'), input: { function: 'setTicker', @@ -44,7 +38,7 @@ describe('setTicker', () => { it.each([undefined, Infinity, 1, 0, -1, 1.1, true, false, null, {}, []])( 'should throw on bad ticker', async (ticker: any) => { - const result = await setTicker(state, { + const result = await setTicker(baselineAntState, { caller: 'test', input: { function: 'setTicker', @@ -56,7 +50,7 @@ describe('setTicker', () => { ); it('should set ticker as owner', async () => { - const result = (await setTicker(state, { + const result = (await setTicker(baselineAntState, { caller: 'test', input: { function: 'setTicker', @@ -67,8 +61,11 @@ describe('setTicker', () => { }); it('should set ticker as controller', async () => { - const _state = { ...state, controllers: [''.padEnd(43, '1')] }; - const result = (await setTicker(_state, { + const initState = { + ...baselineAntState, + controllers: [''.padEnd(43, '1')], + }; + const result = (await setTicker(initState, { caller: ''.padEnd(43, '1'), input: { function: 'setTicker', diff --git a/src/actions/write/transfer.test.ts b/src/actions/write/transfer.test.ts index 72c6200..c1c1d21 100644 --- a/src/actions/write/transfer.test.ts +++ b/src/actions/write/transfer.test.ts @@ -23,12 +23,6 @@ import { baselineAntState } from '../../../tests/utils/constants'; import { transferTokens } from './transferTokens'; describe('transferTokens', () => { - let state = { ...baselineAntState }; - - beforeEach(() => { - state = { ...baselineAntState }; - }); - it.each([ undefined, false, @@ -39,8 +33,8 @@ describe('transferTokens', () => { '?'.padEnd(42, '1'), ''.padEnd(44, '1'), ])('should throw on invalid recipient address', async (target: any) => { - const _state = { ...state }; - const result = await transferTokens(_state, { + const initState = { ...baselineAntState }; + const result = await transferTokens(initState, { caller: 'test', input: { function: 'transfer', @@ -53,12 +47,12 @@ describe('transferTokens', () => { it.each(['23e', null, undefined])( 'should throw on invalid balance type', async (bal: any) => { - const _state = { - ...state, + const initState = { + ...baselineAntState, owner: ''.padEnd(43, '1'), balances: { [''.padEnd(43, '1')]: bal }, }; - const result = await transferTokens(_state, { + const result = await transferTokens(initState, { caller: ''.padEnd(43, '1'), input: { function: 'transfer', @@ -70,12 +64,12 @@ describe('transferTokens', () => { ); it('should throw on insufficient balance', async () => { - const _state = { - ...state, + const initState = { + ...baselineAntState, owner: ''.padEnd(43, '1'), balances: { [''.padEnd(43, '1')]: 0.1 }, }; - const result = await transferTokens(_state, { + const result = await transferTokens(initState, { caller: ''.padEnd(43, '1'), input: { function: 'transfer', @@ -86,12 +80,12 @@ describe('transferTokens', () => { }); it('should throw on transfer to self', async () => { - const _state = { - ...state, + const initState = { + ...baselineAntState, owner: ''.padEnd(43, '1'), balances: { [''.padEnd(43, '1')]: 1 }, }; - const result = await transferTokens(_state, { + const result = await transferTokens(initState, { caller: ''.padEnd(43, '1'), input: { function: 'transfer', @@ -102,8 +96,11 @@ describe('transferTokens', () => { }); it('should throw if not contract owner', async () => { - const _state = { ...state, balances: { [''.padEnd(43, '1')]: 1 } }; - const result = await transferTokens(_state, { + const initState = { + ...baselineAntState, + balances: { [''.padEnd(43, '1')]: 1 }, + }; + const result = await transferTokens(initState, { caller: ''.padEnd(43, '1'), input: { function: 'transfer', @@ -114,8 +111,8 @@ describe('transferTokens', () => { }); it('should transfer ownership', async () => { - const result = (await transferTokens(state, { - caller: state.owner, + const result = (await transferTokens(baselineAntState, { + caller: baselineAntState.owner, input: { function: 'transfer', target: ''.padEnd(43, '1'),