Skip to content

Commit

Permalink
patch(vest): reduceTests
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Jun 8, 2024
1 parent e6380eb commit 38ef2f7
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/vest/src/core/isolate/IsolateTest/TestWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class TestWalker {

static someTests(
predicate: (test: TIsolateTest) => boolean,
root: MaybeRoot = TestWalker.defaultRoot()
root: MaybeRoot = TestWalker.defaultRoot(),
): boolean {
if (!root) return false;
return Walker.some(
Expand All @@ -28,13 +28,13 @@ export class TestWalker {

return predicate(isolate);
},
VestTest.is
VestTest.is,
);
}

static everyTest(
predicate: (test: TIsolateTest) => boolean,
root: MaybeRoot = TestWalker.defaultRoot()
root: MaybeRoot = TestWalker.defaultRoot(),
): boolean {
if (!root) return false;
return Walker.every(
Expand All @@ -44,27 +44,43 @@ export class TestWalker {

return predicate(isolate);
},
VestTest.is
VestTest.is,
);
}

static walkTests<F extends TFieldName, G extends TGroupName>(
callback: (test: TIsolateTest<F, G>, breakout: () => void) => void,
root: MaybeRoot = TestWalker.defaultRoot()
root: MaybeRoot = TestWalker.defaultRoot(),
): void {
if (!root) return;
Walker.walk(
root,
(isolate, breakout) => {
callback(VestTest.cast<F, G>(isolate), breakout);
},
VestTest.is
VestTest.is,
);
}

static reduceTests<T, I extends TIsolateTest = TIsolateTest>(
callback: (acc: T, test: I, breakout: () => void) => T,
initialValue: T,
root: MaybeRoot = TestWalker.defaultRoot(),
): T {
if (!root) return initialValue;
return Walker.reduce(
root,
(acc, isolate, breakout) => {
return callback(acc, VestTest.cast(isolate) as I, breakout);
},
initialValue,
VestTest.is,
);
}

static pluckTests(
predicate: (test: TIsolateTest) => boolean,
root: MaybeRoot = TestWalker.defaultRoot()
root: MaybeRoot = TestWalker.defaultRoot(),
): void {
if (!root) return;
Walker.pluck(
Expand All @@ -74,7 +90,7 @@ export class TestWalker {

return predicate(isolate);
},
VestTest.is
VestTest.is,
);
}

Expand All @@ -88,7 +104,7 @@ export class TestWalker {

static removeTestByFieldName(
fieldName: TFieldName,
root: MaybeRoot = TestWalker.defaultRoot()
root: MaybeRoot = TestWalker.defaultRoot(),
): void {
TestWalker.pluckTests(testObject => {
return matchingFieldName(VestTest.getData(testObject), fieldName);
Expand Down

0 comments on commit 38ef2f7

Please sign in to comment.