To test your getters and setters, @joystick.js/test
includes test.api.get()
and test.api.set()
methods to call endpoints directly.
Testing a Getter
To test a getter, the test.api.get()
method can be utilized:
/tests/api/books/getters.js
import test from '@joystick.js/test';
test.that('the book getter returns a book', async (assert = {}) => {
const data = await test.api.get('book', {
input: {
title: 'Tough and Competent',
}
});
assert.is(data?.author, 'Gene Kranz');
});
The test.api.get()
method is analogous to the get()
method exported from @joystick.js/ui
. To call a getter, we first pass the name of the getter book
as the first argument and then, as the second argument, we pass an options object. Here, we've set the input
value in our options to get a book with a title
of Tough and Competent
.
In response, we get the exact output of our getter endpoint. To verify our getter worked, we assert that the author
field on the resulting data returned by the book
getter matches the author we expect.
Testing a Setter
To test a setter, the test.api.set()
method can be utilized:
/tests/api/books/setters.js
import test from '@joystick.js/test';
test.that('the create_book setter creates a book', async (assert = {}) => {
await test.api.set('create_book', {
input: {
title: 'Tough and Competent',
author: 'Gene Kranz',
}
});
const book = await process.databases.mongodb.collection('books').findOne({
title: 'Tough and Competent',
author: 'Gene Kranz',
});
assert.is(book?.author, 'Gene Kranz');
});
The test.api.set()
method is analogous to the set()
method exported from @joystick.js/ui
. To call a setter, we first pass the name of the setter create_book
as the first argument and then, as the second argument, we pass an options object. Here, we've set the input
value in our options with the title
and author
of a book we'd like to create.
To verify our setter worked, we run a database query to get a book with the title
and author
we passed and assert that the author
field on the resulting data returned by that query matches the author we expect.
Passing a User
For both the test.api.get()
and test.api.set()
methods, via the options object, a user can be passed along with the request:
/tests/api/books/getters.js
import test from '@joystick.js/test';
test.that('the create_book setter creates a book for the user', async (assert = {}) => {
const user = await test.accounts.signup({
email_address: 'example@test.com',
password: 'password',
metadata: {
name: 'Ryan Glover',
},
});
const data = await test.api.set('create_book', {
user,
input: {
title: 'Tough and Competent',
author: 'Gene Kranz',
}
});
const book = await process.databases.mongodb.collection('books').findOne({
title: 'Tough and Competent',
author: 'Gene Kranz',
});
assert.is(book?.owner === user?._id, true);
});
In the example above, we assume that our setter create_book
will automatically assign the logged in user's _id
as the value of owner
on the book we create.
API
test.api.get()
test.api.get(getter_name: string, options: object) => Promise;
Parameters
- getter_name string
- The name of a getter as a string to call in your API.
- options object
- The options for the getter request.
test.api.set()
test.api.set(setter_name: string, options: object) => Promise;
Parameters
- setter_name string
- The name of a setter as a string to call in your API.
- options object
- The options for the setter request.