Skip to content

Commit

Permalink
chore: refactor watch processor test to reduce redundancy (#1763)
Browse files Browse the repository at this point in the history
## Description
Resolve max-statements warning in
src/lib/processors/watch-processor.test.ts
...

End to End Test:  <!-- if applicable -->  
(See [Pepr Excellent
Examples](https://github.com/defenseunicorns/pepr-excellent-examples))

## Related Issue

Fixes #1696 


## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Other (security config, docs update, etc)

## Checklist before merging
- [ ] Unit,
[Journey](https://github.com/defenseunicorns/pepr/tree/main/journey),
[E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples),
[docs](https://github.com/defenseunicorns/pepr/tree/main/docs),
[adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or
updated as needed
- [x] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request)
followed

---------

Co-authored-by: Sam Mayer <[email protected]>
Co-authored-by: Case Wylie <[email protected]>
  • Loading branch information
3 people authored Feb 7, 2025
1 parent 517bbd6 commit 129376b
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions src/lib/processors/watch-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ jest.mock("../telemetry/metrics", () => ({
incRetryCount: jest.fn(),
},
}));

const testPhaseCallbacks = (
mockCallback: (payload: kind.Pod, phase: WatchPhase) => void,
watchCallback: jest.Mock,
phase: WatchPhase,
cbNotCalled: {
callback: jest.Mock;
phase: WatchPhase;
}[],
) => {
mockCallback({} as kind.Pod, phase);

expect(watchCallback).toHaveBeenCalledTimes(1);
expect(watchCallback).toHaveBeenCalledWith({}, phase);

cbNotCalled.forEach(({ callback, phase }) => {
mockCallback({} as kind.Pod, phase);
expect(callback).not.toHaveBeenCalled();
});
};

describe("WatchProcessor", () => {
const mockStart = jest.fn();
const mockK8s = jest.mocked(K8s);
Expand Down Expand Up @@ -175,48 +196,34 @@ describe("WatchProcessor", () => {

type mockArg = [(payload: kind.Pod, phase: WatchPhase) => void, WatchCfg];

const firstCall = mockWatch.mock.calls[0] as unknown as mockArg;
const secondCall = mockWatch.mock.calls[1] as unknown as mockArg;
const thirdCall = mockWatch.mock.calls[2] as unknown as mockArg;
const [firstCall, secondCall, thirdCall] = mockWatch.mock.calls as unknown as mockArg[];

expect(firstCall[1].resyncFailureMax).toEqual(5);
expect(firstCall[1].resyncDelaySec).toEqual(5);
expect(firstCall[0]).toBeInstanceOf(Function);

firstCall[0]({} as kind.Pod, WatchPhase.Added);
expect(watchCallbackCreate).toHaveBeenCalledTimes(1);
expect(watchCallbackCreate).toHaveBeenCalledWith({}, WatchPhase.Added);

firstCall[0]({} as kind.Pod, WatchPhase.Modified);
firstCall[0]({} as kind.Pod, WatchPhase.Deleted);
expect(watchCallbackDelete).toHaveBeenCalledTimes(0);
expect(watchCallbackUpdate).toHaveBeenCalledTimes(0);
testPhaseCallbacks(firstCall[0], watchCallbackCreate, WatchPhase.Added, [
{ callback: watchCallbackUpdate, phase: WatchPhase.Modified },
{ callback: watchCallbackDelete, phase: WatchPhase.Deleted },
]);

watchCallbackCreate.mockClear();
watchCallbackUpdate.mockClear();
watchCallbackDelete.mockClear();

secondCall[0]({} as kind.Pod, WatchPhase.Modified);
expect(watchCallbackUpdate).toHaveBeenCalledTimes(1);
expect(watchCallbackUpdate).toHaveBeenCalledWith({}, WatchPhase.Modified);

secondCall[0]({} as kind.Pod, WatchPhase.Added);
secondCall[0]({} as kind.Pod, WatchPhase.Deleted);
expect(watchCallbackCreate).toHaveBeenCalledTimes(0);
expect(watchCallbackDelete).toHaveBeenCalledTimes(0);
testPhaseCallbacks(secondCall[0], watchCallbackUpdate, WatchPhase.Modified, [
{ callback: watchCallbackCreate, phase: WatchPhase.Added },
{ callback: watchCallbackDelete, phase: WatchPhase.Deleted },
]);

watchCallbackCreate.mockClear();
watchCallbackUpdate.mockClear();
watchCallbackDelete.mockClear();

thirdCall[0]({} as kind.Pod, WatchPhase.Deleted);
expect(watchCallbackDelete).toHaveBeenCalledTimes(1);
expect(watchCallbackDelete).toHaveBeenCalledWith({}, WatchPhase.Deleted);

thirdCall[0]({} as kind.Pod, WatchPhase.Added);
thirdCall[0]({} as kind.Pod, WatchPhase.Modified);
expect(watchCallbackCreate).toHaveBeenCalledTimes(0);
expect(watchCallbackUpdate).toHaveBeenCalledTimes(0);
testPhaseCallbacks(thirdCall[0], watchCallbackDelete, WatchPhase.Deleted, [
{ callback: watchCallbackCreate, phase: WatchPhase.Added },
{ callback: watchCallbackUpdate, phase: WatchPhase.Modified },
]);
});

it("should call the metricsCollector methods on respective events", async () => {
Expand Down

0 comments on commit 129376b

Please sign in to comment.