forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): Add
describeScenario
utility to group scenario tests (…
…redwoodjs#9866) Co-authored-by: Curtis Reimer <51102303+cjreimer@users.noreply.github.com>
- Loading branch information
Showing
9 changed files
with
395 additions
and
40 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
__fixtures__/test-project/api/src/services/contacts/describeContacts.scenarios.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Prisma, Contact } from '@prisma/client' | ||
|
||
import type { ScenarioData } from '@redwoodjs/testing/api' | ||
|
||
export const standard = defineScenario<Prisma.ContactCreateArgs>({ | ||
contact: { | ||
one: { data: { name: 'String', email: 'String', message: 'String' } }, | ||
two: { data: { name: 'String', email: 'String', message: 'String' } }, | ||
}, | ||
}) | ||
|
||
export type StandardScenario = ScenarioData<Contact, 'contact'> |
57 changes: 57 additions & 0 deletions
57
__fixtures__/test-project/api/src/services/contacts/describeContacts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { db } from 'src/lib/db' | ||
|
||
import { contact, contacts, createContact } from './contacts' | ||
import type { StandardScenario } from './contacts.scenarios' | ||
|
||
/** | ||
* Example test for describe scenario. | ||
* | ||
* Note that scenario tests need a matching [name].scenarios.ts file. | ||
*/ | ||
|
||
describeScenario<StandardScenario>('contacts', (getScenario) => { | ||
let scenario: StandardScenario | ||
|
||
beforeEach(() => { | ||
scenario = getScenario() | ||
}) | ||
|
||
it('returns all contacts', async () => { | ||
const result = await contacts() | ||
|
||
expect(result.length).toEqual(Object.keys(scenario.contact).length) | ||
}) | ||
|
||
it('returns a single contact', async () => { | ||
const result = await contact({ id: scenario.contact.one.id }) | ||
|
||
expect(result).toEqual(scenario.contact.one) | ||
}) | ||
|
||
it('creates a contact', async () => { | ||
const result = await createContact({ | ||
input: { | ||
name: 'Bazinga', | ||
email: 'contact@describe.scenario', | ||
message: 'Describe scenario works!', | ||
}, | ||
}) | ||
|
||
expect(result.name).toEqual('Bazinga') | ||
expect(result.email).toEqual('contact@describe.scenario') | ||
expect(result.message).toEqual('Describe scenario works!') | ||
}) | ||
|
||
it('Checking that describe scenario works', async () => { | ||
// This test is dependent on the above test. If you used a normal scenario it would not work | ||
const contactCreatedInAboveTest = await db.contact.findFirst({ | ||
where: { | ||
email: 'contact@describe.scenario', | ||
}, | ||
}) | ||
|
||
expect(contactCreatedInAboveTest.message).toEqual( | ||
'Describe scenario works!' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/internal/src/generate/templates/api-scenarios.d.ts.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import type { Scenario, DefineScenario } from '@redwoodjs/testing/api' | ||
import type { Scenario, DefineScenario, DescribeScenario } from '@redwoodjs/testing/api' | ||
|
||
declare global { | ||
/** | ||
* Note that the scenario name must match the exports in your {model}.scenarios.ts file | ||
*/ | ||
const scenario: Scenario | ||
const describeScenario: DescribeScenario | ||
const defineScenario: DefineScenario | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.