Skip to content

Commit 59b3bee

Browse files
amorimjulianamarcospassos
authored andcommitted
Add support for testing boolean expressions
1 parent 4eaab8a commit 59b3bee

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/plug.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ export class GlobalPlug implements Plug {
280280
return this.sdk.evaluate(expression, options);
281281
}
282282

283+
public test(expression: string, options: EvaluationOptions = {}): Promise<boolean> {
284+
return this.evaluate(expression, options)
285+
.then(result => result === true);
286+
}
287+
283288
public async unplug(): Promise<void> {
284289
if (this.instance === undefined) {
285290
return;

test/plug.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,63 @@ describe('The Croct plug', () => {
602602
expect(() => croct.evaluate('foo', {timeout: 5})).toThrow('Croct is not plugged in.');
603603
});
604604

605+
test('should allow to test expressions', async () => {
606+
const config: SdkFacadeConfiguration = {appId: APP_ID};
607+
const sdkFacade = SdkFacade.init(config);
608+
609+
const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);
610+
611+
croct.plug(config);
612+
613+
expect(initialize).toBeCalledWith(config);
614+
615+
const evaluate = jest.spyOn(sdkFacade, 'evaluate').mockResolvedValue(true);
616+
617+
const promise = croct.test('user\'s name is "Carol"', {timeout: 5});
618+
619+
await expect(promise).resolves.toBe(true);
620+
621+
expect(evaluate).toBeCalledWith('user\'s name is "Carol"', {timeout: 5});
622+
});
623+
624+
test('should test expressions assuming non-boolean results as false', async () => {
625+
const config: SdkFacadeConfiguration = {appId: APP_ID};
626+
const sdkFacade = SdkFacade.init(config);
627+
628+
const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);
629+
630+
croct.plug(config);
631+
632+
expect(initialize).toBeCalledWith(config);
633+
634+
const evaluate = jest.spyOn(sdkFacade, 'evaluate').mockResolvedValue('foo');
635+
636+
const promise = croct.test('user\'s name is "Carol"', {timeout: 5});
637+
638+
await expect(promise).resolves.toBe(false);
639+
640+
expect(evaluate).toBeCalledWith('user\'s name is "Carol"', {timeout: 5});
641+
});
642+
643+
test('should not test expressions assuming errors as false', async () => {
644+
const config: SdkFacadeConfiguration = {appId: APP_ID};
645+
const sdkFacade = SdkFacade.init(config);
646+
647+
const initialize = jest.spyOn(SdkFacade, 'init').mockReturnValue(sdkFacade);
648+
649+
croct.plug(config);
650+
651+
expect(initialize).toBeCalledWith(config);
652+
653+
const evaluate = jest.spyOn(sdkFacade, 'evaluate').mockRejectedValue(undefined);
654+
655+
const promise = croct.test('user\'s name is "Carol"', {timeout: 5});
656+
657+
await expect(promise).rejects.toBeUndefined();
658+
659+
expect(evaluate).toBeCalledWith('user\'s name is "Carol"', {timeout: 5});
660+
});
661+
605662
test('should wait for the plugins to disable before closing the SDK', async () => {
606663
let unloadFooPlugin: () => void = jest.fn();
607664
const fooDisable = jest.fn().mockImplementation(() => new Promise<void>(resolve => {

0 commit comments

Comments
 (0)