Skip to content

Commit

Permalink
🧪 [Tests] More consistent use of async for each test
Browse files Browse the repository at this point in the history
  • Loading branch information
beefchimi committed Dec 27, 2023
1 parent 64e5610 commit 82b7300
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
16 changes: 8 additions & 8 deletions src/tests/Abstract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Abstract implementation', () => {
mockSound.mute = false;
});

it('allows `set` and `get`', () => {
it('allows `set` and `get`', async () => {
expect(mockSound.mute).toBe(false);
mockSound.mute = true;
expect(mockSound.mute).toBe(true);
Expand All @@ -40,7 +40,7 @@ describe('Abstract implementation', () => {
it.todo('sets gain to 0 when muted');
it.todo('sets gain to volume when un-muted');

it('fades to new value', () => {
it('fades to new value', async () => {
const mockOptions: SoundConfig = {
volume: 0.6,
fadeMs: 100,
Expand Down Expand Up @@ -105,24 +105,24 @@ describe('Abstract implementation', () => {
mockSound.mute = false;
});

it('allows `set` and `get`', () => {
it('allows `set` and `get`', async () => {
const mockVolume = 0.4;
mockSound.volume = mockVolume;

expect(mockSound.volume).toBe(mockVolume);
});

it('restricts value to a minimum of `0`', () => {
it('restricts value to a minimum of `0`', async () => {
mockSound.volume = -2;
expect(mockSound.volume).toBe(0);
});

it('restricts value to a maximum of `1`', () => {
it('restricts value to a maximum of `1`', async () => {
mockSound.volume = 2;
expect(mockSound.volume).toBe(1);
});

it('sets value on `gain`', () => {
it('sets value on `gain`', async () => {
const oldValue = mockSound.volume;
const newValue = 0.6;
const {currentTime} = defaultContext;
Expand All @@ -145,7 +145,7 @@ describe('Abstract implementation', () => {
expect(spyGainRamp).toBeCalledWith(newValue, currentTime);
});

it('does not set value on gain if muted', () => {
it('does not set value on gain if muted', async () => {
const mockVolume = 0.2;

const spyGainCancel = vi.spyOn(
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Abstract implementation', () => {
expect(spyGainRamp).not.toBeCalledTimes(2);
});

it('fades to new volume', () => {
it('fades to new volume', async () => {
const mockOptions: SoundConfig = {
volume: 0.6,
fadeMs: 100,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/Earwurm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Earwurm component', () => {
});

describe('initialization', () => {
it('is initialized with default values', () => {
it('is initialized with default values', async () => {
expect(mockManager).toBeInstanceOf(Earwurm);

// Class static properties
Expand Down
2 changes: 1 addition & 1 deletion src/tests/Stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('Stack component', () => {
});

describe('initialization', () => {
it('is initialized with default values', () => {
it('is initialized with default values', async () => {
expect(mockStack).toBeInstanceOf(Stack);

// Class static properties
Expand Down
14 changes: 7 additions & 7 deletions src/tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Helpers', () => {
const mockContext = new AudioContext();

describe('getErrorMessage()', () => {
it('returns message from basic object', () => {
it('returns message from basic object', async () => {
const mockError = {
message: 'foo',
};
Expand All @@ -23,7 +23,7 @@ describe('Helpers', () => {
expect(result).toBe(mockError.message);
});

it('returns message from Error', () => {
it('returns message from Error', async () => {
const mockMessage = 'Foo';
const mockError = new Error(mockMessage, {
cause: 'bar',
Expand All @@ -34,7 +34,7 @@ describe('Helpers', () => {
expect(result).toBe(mockMessage);
});

it('returns stringified result when unknown', () => {
it('returns stringified result when unknown', async () => {
const mockError = ['foo', true, {bar: false}, null];
const result = getErrorMessage(mockError);

Expand Down Expand Up @@ -67,7 +67,7 @@ describe('Helpers', () => {
});

describe('linearRamp()', () => {
it('transitions to the specified value', () => {
it('transitions to the specified value', async () => {
const mockAudioParam = new AudioParam();

const spyCancel = vi.spyOn(mockAudioParam, 'cancelScheduledValues');
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('Helpers', () => {
});

describe('scratchBuffer()', () => {
it('creates a short silent AudioBuffer', () => {
it('creates a short silent AudioBuffer', async () => {
const result = scratchBuffer(mockContext);

expect(result).toBeInstanceOf(AudioBuffer);
Expand All @@ -120,7 +120,7 @@ describe('Helpers', () => {
vi.advanceTimersToNextTimer();
});

it('resumes AudioContext state', () => {
it('resumes AudioContext state', async () => {
const spyCreateBuffer = vi.spyOn(mockContext, 'createBuffer');
const spyResume = vi.spyOn(mockContext, 'resume');

Expand All @@ -147,7 +147,7 @@ describe('Helpers', () => {
expect(spySourceStart).toBeCalledTimes(1);
});

it('calls onEnded after interaction event', () => {
it('calls onEnded after interaction event', async () => {
vi.spyOn(
AudioBufferSourceNode.prototype,
'addEventListener',
Expand Down
38 changes: 19 additions & 19 deletions src/tests/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
describe('Utilities', () => {
describe('Array', () => {
describe('arrayOfLength()', () => {
it('returns an empty array by default', () => {
it('returns an empty array by default', async () => {
const result = arrayOfLength();
expect(result).toStrictEqual([]);
});

it('returns an array of incremented index values', () => {
it('returns an array of incremented index values', async () => {
const mockLength = 6;
const result = arrayOfLength(mockLength);

Expand All @@ -27,15 +27,15 @@ describe('Utilities', () => {
});

describe('arrayShallowEquals()', () => {
it('returns `true` when matching', () => {
it('returns `true` when matching', async () => {
const result = arrayShallowEquals(
[true, false, null, undefined, 0, 1, 'end'],
[true, false, null, undefined, 0, 1, 'end'],
);
expect(result).toBe(true);
});

it('returns `false` when at least one value is unmatched', () => {
it('returns `false` when at least one value is unmatched', async () => {
const result = arrayShallowEquals([true, false], [false, true]);
expect(result).toBe(false);
});
Expand All @@ -44,32 +44,32 @@ describe('Utilities', () => {

describe('Numbers', () => {
describe('assertNumber()', () => {
it('accepts an integer', () => {
it('accepts an integer', async () => {
const result = assertNumber(123);
expect(result).toBe(true);
});

it('accepts a float', () => {
it('accepts a float', async () => {
const result = assertNumber(123.456);
expect(result).toBe(true);
});

it('does not allow a bigint', () => {
it('does not allow a bigint', async () => {
const result = assertNumber(1000n);
expect(result).toBe(false);
});

it('does not allow NaN', () => {
it('does not allow NaN', async () => {
const result = assertNumber(NaN);
expect(result).toBe(false);
});

it('does not allow Infinity', () => {
it('does not allow Infinity', async () => {
const result = assertNumber(Infinity);
expect(result).toBe(false);
});

it('does not allow other non-numnber types', () => {
it('does not allow other non-numnber types', async () => {
expect(assertNumber(true)).toBe(false);
expect(assertNumber(false)).toBe(false);
expect(assertNumber({one: 1})).toBe(false);
Expand All @@ -79,21 +79,21 @@ describe('Utilities', () => {
});

describe('clamp()', () => {
it('returns preference', () => {
it('returns preference', async () => {
const mockArgs: Parameters<typeof clamp> = [1, 10, 100];
const result = clamp(...mockArgs);

expect(result).toBe(10);
});

it('returns min', () => {
it('returns min', async () => {
const mockArgs: Parameters<typeof clamp> = [10, 1, 100];
const result = clamp(...mockArgs);

expect(result).toBe(10);
});

it('returns max', () => {
it('returns max', async () => {
const mockArgs: Parameters<typeof clamp> = [1, 100, 10];
const result = clamp(...mockArgs);

Expand All @@ -102,24 +102,24 @@ describe('Utilities', () => {
});

describe('progressPercentage()', () => {
it('returns percentage integer', () => {
it('returns percentage integer', async () => {
const result = progressPercentage(6, 20);
expect(result).toBe(30);
});

it('round down floating-point result', () => {
it('round down floating-point result', async () => {
const result = progressPercentage(12, 345);
// Before Math.floor(), result should be:
// 3.4782608695652173
expect(result).toBe(3);
});

it('protects against NaN', () => {
it('protects against NaN', async () => {
const result = progressPercentage(0, 0);
expect(result).toBe(0);
});

it('protects against Infinity', () => {
it('protects against Infinity', async () => {
const result = progressPercentage(2, 0);
expect(result).toBe(0);
});
Expand All @@ -128,14 +128,14 @@ describe('Utilities', () => {

describe('Time', () => {
describe('msToSec()', () => {
it('converts to seconds', () => {
it('converts to seconds', async () => {
const result = msToSec(1234);
expect(result).toBe(1.234);
});
});

describe('secToMs()', () => {
it('converts to milliseconds', () => {
it('converts to milliseconds', async () => {
const result = secToMs(5.678);
expect(result).toBe(5678);
});
Expand Down

0 comments on commit 82b7300

Please sign in to comment.