Skip to content

Commit

Permalink
Merge pull request #396 from thefrontside/resolve-means-success
Browse files Browse the repository at this point in the history
When Interactor promise resolves, it implies success
  • Loading branch information
cowboyd authored Jul 9, 2020
2 parents da93513 + 4c695f0 commit 34a29e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changeset/resolved-implies-success.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bigtest/interactor": minor
---
Drop resolve value from interactions and assertions. If the promise
resolves, that means it was successful. in other words, the type of
`exists()` is now `() => Promise<void>`, not `() => Promise<true>`
13 changes: 6 additions & 7 deletions packages/interactor/src/interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,44 @@ export class Interactor<E extends Element, S extends InteractorSpecification<E>>
});
}

exists(): Interaction<true> {
exists(): Interaction<void> {
return interaction(`${this.description} exists`, () => {
return converge(() => {
this.unsafeSyncResolve();
return true;
});
});
}

absent(): Interaction<true> {
absent(): Interaction<void> {
return interaction(`${this.description} does not exist`, () => {
return converge(() => {
try {
this.unsafeSyncResolve();
} catch(e) {
if(e.name === 'NoSuchElementError') {
return true;
return;
}
}
throw new NotAbsentError(`${this.description} exists but should not`);
});
});
}

is(filters: FilterImplementation<E, S>): Interaction<true> {
is(filters: FilterImplementation<E, S>): Interaction<void> {
let filter = new Filter(this.specification, filters);
return interaction(`${this.description} matches filters: ${filter.description}`, () => {
return converge(() => {
let element = this.unsafeSyncResolve();
if(filter.matches(element)) {
return true;
return;
} else {
throw new FilterNotMatchingError(`${this.description} does not match filters: ${filter.description}`);
}
});
});
}

has(filters: FilterImplementation<E, S>): Interaction<true> {
has(filters: FilterImplementation<E, S>): Interaction<void> {
return this.is(filters);
}
}
32 changes: 16 additions & 16 deletions packages/interactor/test/create-interactor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('@bigtest/interactor', () => {
<p><a href="/foobar">Foo Bar</a></p>
`);

await expect(Link('Foo Bar').exists()).resolves.toEqual(true);
await expect(Link('Foo Bar').exists()).resolves.toBeUndefined();
await expect(Link('Blah').exists()).rejects.toHaveProperty('message', 'link "Blah" does not exist');
});

Expand All @@ -72,7 +72,7 @@ describe('@bigtest/interactor', () => {
<p><a title="Monkey" href="/foobar">Foo Bar</a></p>
`);

await expect(Link.byTitle('Monkey').exists()).resolves.toEqual(true);
await expect(Link.byTitle('Monkey').exists()).resolves.toBeUndefined();
await expect(Link.byTitle('Zebra').exists()).rejects.toHaveProperty('message', 'link with title "Zebra" does not exist');
});

Expand All @@ -86,7 +86,7 @@ describe('@bigtest/interactor', () => {
</script>
`);

await expect(Link('Foo Bar').exists()).resolves.toEqual(true);
await expect(Link('Foo Bar').exists()).resolves.toBeUndefined();
});

it('can return description', () => {
Expand All @@ -100,7 +100,7 @@ describe('@bigtest/interactor', () => {
<p><a href="/foobar">Foo Bar</a></p>
`);

await expect(Link('Blah').absent()).resolves.toEqual(true);
await expect(Link('Blah').absent()).resolves.toBeUndefined();
await expect(Link('Foo Bar').absent()).rejects.toHaveProperty('message', 'link "Foo Bar" exists but should not');
});

Expand All @@ -114,7 +114,7 @@ describe('@bigtest/interactor', () => {
</script>
`);

await expect(Link('Foo Bar').absent()).resolves.toEqual(true);
await expect(Link('Foo Bar').absent()).resolves.toBeUndefined();
});

it('can return description', () => {
Expand All @@ -133,8 +133,8 @@ describe('@bigtest/interactor', () => {
</div>
`);

await expect(Div("foo").find(Link("Foo")).exists()).resolves.toEqual(true);
await expect(Div("bar").find(Link("Bar")).exists()).resolves.toEqual(true);
await expect(Div("foo").find(Link("Foo")).exists()).resolves.toBeUndefined();
await expect(Div("bar").find(Link("Bar")).exists()).resolves.toBeUndefined();

await expect(Div("foo").find(Link("Bar")).exists()).rejects.toHaveProperty('message', 'link "Bar" within div "foo" does not exist');
await expect(Div("bar").find(Link("Foo")).exists()).rejects.toHaveProperty('message', 'link "Foo" within div "bar" does not exist');
Expand Down Expand Up @@ -175,10 +175,10 @@ describe('@bigtest/interactor', () => {
<a href="/foo">Foo</a>
`);

await expect(Div("test").find(Div("foo").find(Link("Foo"))).exists()).resolves.toEqual(true);
await expect(Div("test").find(Div("foo").find(Link("Foo"))).exists()).resolves.toBeUndefined();
await expect(Div("test").find(Div("foo").find(Link("Bar"))).exists()).rejects.toHaveProperty('message', 'link "Bar" within div "foo" within div "test" does not exist');

await expect(Div("test").find(Div("foo")).find(Link("Foo")).exists()).resolves.toEqual(true);
await expect(Div("test").find(Div("foo")).find(Link("Foo")).exists()).resolves.toBeUndefined();
await expect(Div("test").find(Div("foo")).find(Link("Bar")).exists()).rejects.toHaveProperty('message', 'link "Bar" within div "foo" within div "test" does not exist');
});
});
Expand All @@ -189,7 +189,7 @@ describe('@bigtest/interactor', () => {
<input id="Email" value='[email protected]'/>
`);

await expect(TextField('Email').is({ value: '[email protected]' })).resolves.toEqual(true);
await expect(TextField('Email').is({ value: '[email protected]' })).resolves.toBeUndefined();
await expect(TextField('Email').is({ value: '[email protected]' })).rejects.toHaveProperty('message', 'text field "Email" does not match filters: with value "[email protected]"');
});
});
Expand All @@ -200,7 +200,7 @@ describe('@bigtest/interactor', () => {
<input id="Email" value='[email protected]'/>
`);

await expect(TextField('Email').has({ value: '[email protected]' })).resolves.toEqual(true);
await expect(TextField('Email').has({ value: '[email protected]' })).resolves.toBeUndefined();
await expect(TextField('Email').has({ value: '[email protected]' })).rejects.toHaveProperty('message', 'text field "Email" does not match filters: with value "[email protected]"');
});
});
Expand Down Expand Up @@ -257,7 +257,7 @@ describe('@bigtest/interactor', () => {
`);

await Div("foo").find(Link('Foo Bar')).click();
await expect(Header('Hello!').exists()).resolves.toEqual(true);
await expect(Header('Hello!').exists()).resolves.toBeUndefined();
});

it('can return description of interaction', () => {
Expand All @@ -275,8 +275,8 @@ describe('@bigtest/interactor', () => {
<input id="Email" value='[email protected]'/>
`);

await expect(TextField('Email').exists()).resolves.toEqual(true);
await expect(TextField('Email', { value: '[email protected]' }).exists()).resolves.toEqual(true);
await expect(TextField('Email').exists()).resolves.toBeUndefined();
await expect(TextField('Email', { value: '[email protected]' }).exists()).resolves.toBeUndefined();
await expect(TextField('Email', { value: '[email protected]' }).exists()).rejects.toHaveProperty('message', 'text field "Email" with value "[email protected]" does not exist');
});

Expand All @@ -288,7 +288,7 @@ describe('@bigtest/interactor', () => {

await expect(TextField('Password').exists()).rejects.toHaveProperty('message', 'text field "Password" does not exist');
await expect(TextField('Password', { enabled: true }).exists()).rejects.toHaveProperty('message', 'text field "Password" which is enabled does not exist');
await expect(TextField('Password', { enabled: false }).exists()).resolves.toEqual(true);
await expect(TextField('Password', { enabled: false }).exists()).resolves.toBeUndefined();
});

it('can apply multiple filters', async () => {
Expand All @@ -299,7 +299,7 @@ describe('@bigtest/interactor', () => {

await expect(TextField('Password', { enabled: false, value: 'incorrect' }).exists()).rejects.toHaveProperty('message', 'text field "Password" which is not enabled and with value "incorrect" does not exist');
await expect(TextField('Password', { enabled: true, value: 'test1234' }).exists()).rejects.toHaveProperty('message', 'text field "Password" which is enabled and with value "test1234" does not exist');
await expect(TextField('Password', { enabled: false, value: 'test1234' }).exists()).resolves.toEqual(true);
await expect(TextField('Password', { enabled: false, value: 'test1234' }).exists()).resolves.toBeUndefined();
});
});
})

0 comments on commit 34a29e5

Please sign in to comment.