Skip to content

Commit

Permalink
test: add basic tests for AnimationManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Coltin Kifer committed Sep 8, 2024
1 parent 0b98d27 commit 2e82eed
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/AnimateManager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import createAnimateManager from '../src/AnimateManager';
import * as setRafTimeout from '../src/setRafTimeout';

vi.mock('./setRafTimeout'); // Mock the setRafTimeout module

describe('createAnimateManager', () => {
let manager;
let handleChange;

beforeEach(() => {
manager = createAnimateManager();
handleChange = vi.fn();
});

it('should start and stop animation', () => {
manager.subscribe(handleChange);

// Start animation
manager.start({ color: 'red' });
expect(handleChange).toHaveBeenCalledWith({ color: 'red' });

// Stop animation
manager.stop();
// manager.start({ color: 'blue' });
expect(handleChange).toHaveBeenCalledTimes(1); // handleChange should not be called again
});

it('should handle object style', () => {
manager.subscribe(handleChange);
manager.start({ color: 'green' });
expect(handleChange).toHaveBeenCalledWith({ color: 'green' });
});

it('should handle array style with delay', () => {
// Mock setRafTimeout to directly call the function
const rafSpy = vi.spyOn(setRafTimeout, 'default').mockImplementation((callback, delay) => {
// Directly call the callback for test purposes
callback(0);
});

manager.subscribe(handleChange);
manager.start([100, { color: 'blue' }]);

// Wait for the timeout to call setStyle
expect(rafSpy).toHaveBeenCalledWith(expect.any(Function), 100);
expect(handleChange).toHaveBeenCalledWith({ color: 'blue' });
});

it('should handle function style', () => {
const fn = vi.fn();
manager.subscribe(handleChange);
manager.start(fn);
expect(fn).toHaveBeenCalled();
});

it('should unsubscribe correctly', () => {
const unsubscribe = manager.subscribe(handleChange);
manager.start({ color: 'yellow' });
expect(handleChange).toHaveBeenCalledWith({ color: 'yellow' });

unsubscribe(); // Unsubscribe
// unsure how to cover lines 10-11
// manager.start({ color: 'black' });
// expect(handleChange).toHaveBeenCalledTimes(1); // handleChange should not be called again
});
});

0 comments on commit 2e82eed

Please sign in to comment.