Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add simple client tests #782

Merged
merged 12 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions client-test-apps/js/api-model-relationship-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# Test Artifacts and interstitials we don't want to persist
test-results.xml
amplify
src/API.ts
src/graphql
.graphqlconfig.yml

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

#amplify-do-not-edit-begin
amplify/\#current-cloud-backend
amplify/.config/local-*
amplify/logs
amplify/mock-data
amplify/backend/amplify-meta.json
amplify/backend/.temp
build/
dist/
node_modules/
aws-exports.js
awsconfiguration.json
amplifyconfiguration.json
amplifyconfiguration.dart
amplify-build-config.json
amplify-gradle-config.json
amplifytools.xcconfig
.secret-*
**.sample
#amplify-do-not-edit-end
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files.exclude": {
"amplify/.config": true,
"amplify/**/*-parameters.json": true,
"amplify/**/amplify.state": true,
"amplify/**/transform.conf.json": true,
"amplify/#current-cloud-backend": true,
"amplify/backend/amplify-meta.json": true,
"amplify/backend/awscloudformation": true
}
}
72 changes: 72 additions & 0 deletions client-test-apps/js/api-model-relationship-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Using this test app

This test app has capabilities to deploy a backend amplify project, tear down said project, and execute a ui test suite against the running app using the full backend.

To facilitate easier dev/test cycles, the `test` there are 4 test lifecycles defined with custom targets to make it easier to develop.

`npm run test` will execute all of these lifecycles.
`npm run test:setup` will strictly setup the amplify app in your DEFAULT aws profile.
`npm run test:teardown` will execute an `amplify delete` on the project.
`npm run test:execute` will start the webapp, and run the cypress suite against it.
`npm run test:watch` will start the app, and open a cypress watch against it.

In addition to these targets, `npm start` can still be used to run the app locally (recommended after `npm run test:setup` to develop new test pages/cases), and `npx cypress open` can be used to interactively run/re-run the cypress suite, this is probably preferable to using just `npm run test:watch` but I don't have a strong opinion on that yet.

## Test Structure

Both the amplify app setup, and basic CRUD+List+Observe functionality rely on test harnesses to reduce the per-test load of creating a UI that uses our API/DataStore clients, and managing basic lifecycle concerns like app setup, teardown, invoking cypress, etc.

Cypress suites are still written by hand in the `cypress` directory, though we could build helpers given the consistent structure of the UI due to these harnesses.

Backend setup code is available at [api.test.ts](src/__tests__/api.test.ts), and top-level app pages can be found in the [pages](src/pages/) directory.

## CLI Interactions

This project uses a stripped down version of the `amplify-e2e-core` shims for shelling out to Amplify. This is expected to be shortly refactored back to a shared lib, but the reason we don't use that directly is due to the large dep-tree it invokes, including the actual CLI source code, which causes issues with jest. This is also potentially mitigable, but while I was under the hood, I simplified the API a bit as well, which I'm happy with. The refactored bits list in the `__tests__/utils` directory, in addition to the test harness package, which is expected to be re-used across apps for consistency.

# About this Repo

This project was bootstrapped with [Create React App](https:/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.\
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "cypress";

export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/// <reference types='cypress' />

const uuid = () => Cypress._.random(0, 1e6)

const PAGE_ROUTE = 'http://localhost:3000/blogs';
const TEST_ID = `${uuid()}`;
const SUCCESS_MARK = '✅';
const FAILURE_MARK = '❌';
const PAGE_TITLE = 'Blog Controls';
const UPDATED_TITLE = `updated blog title ${TEST_ID}`;

/**
* It's not great practice, but these tests rely on being in-order for now.
*/
describe('blog page', () => {
before(() => {
cy.visit(PAGE_ROUTE)
});

describe('page state is stable', () => {
it('loads', () => {
cy.contains(PAGE_TITLE);
})

it('initializes subscriptions', () => {
cy.get('#subscription-state').contains(SUCCESS_MARK);
})
});

describe('blog interactions', () => {
describe('create', () => {
it('creates a blog', () => {
cy.get('#blog-id-input').clear().type(TEST_ID);
cy.get('#blog-create').click();
cy.get('#blog-is-created').within(() => { cy.contains(SUCCESS_MARK) });
});

it('does not allow creation of a blog with identical id', () => {
cy.get('#blog-id-input').clear().type(TEST_ID);
cy.get('#blog-create').click();
cy.get('#blog-is-created').within(() => { cy.contains(FAILURE_MARK) });
});

it('subscription observes blog creation', () => {
cy.get('#created-blogs-subscription').within(() => { cy.contains(TEST_ID) });
});

it('gets a created blog by id', () => {
cy.get('#retrieve-blog-id').clear().type(TEST_ID);
cy.get('#retrieve-blog-button').click();
cy.get('#blog-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-blog').contains(TEST_ID);
});

it('lists the created blogs', () => {
cy.get('#list-blogs').click();
cy.get('#blogs-are-listed').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#listed-blogs').contains(TEST_ID);
});
});

describe('update', () => {
it('updates a blog', () => {
// Load the blog
cy.get('#retrieve-blog-id').clear().type(TEST_ID);
cy.get('#retrieve-blog-button').click();
cy.get('#blog-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-blog').contains(TEST_ID);

// Go to Edit State, Update, and Save
cy.get('#retrieved-blog').within(() => {
cy.contains('Edit').click();
cy.get('#update-title-input').clear().type(UPDATED_TITLE);
cy.get('#update-blog').click();
cy.get('#blog-is-updated').contains(SUCCESS_MARK);
});
});

it('subscription observes blog update', () => {
cy.get('#updated-blogs-subscription').within(() => { cy.contains(UPDATED_TITLE) });
});

it('lists the updated blog', () => {
cy.get('#list-blogs').click();
cy.get('#blogs-are-listed').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#listed-blogs').contains(UPDATED_TITLE);
});
});

describe('delete', () => {
it('deletes a retrieved blog', () => {
// Load the blog
cy.get('#retrieve-blog-id').clear().type(TEST_ID);
cy.get('#retrieve-blog-button').click();
cy.get('#blog-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-blog').contains(TEST_ID);

// Deletes
cy.get('#retrieved-blog').within(() => {
cy.get('#delete-blog').click();
cy.get('#blog-is-updated').contains(SUCCESS_MARK);
});

// Does not reload
cy.get('#retrieve-blog-id').clear().type(TEST_ID);
cy.get('#retrieve-blog-button').click();
cy.get('#blog-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-blog').contains(TEST_ID).should('not.exist');
});

it('subscription observes blog deletion', () => {
cy.get('#deleted-blogs-subscription').within(() => { cy.contains(TEST_ID) });
});

it('no longer gets deleted blog in list', () => {
cy.get('#list-blogs').click();
cy.get('#blogs-are-listed').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#listed-blogs').contains(TEST_ID).should('not.exist');
});
});
});

describe('post interactions', () => {
const BLOG_ID = `${uuid()}`;
const POST_ID_1 = `${uuid()}`;
const POST_ID_2 = `${uuid()}`;

describe('attaches relationship', () => {
it('creates a blog', () => {
cy.get('#blog-id-input').clear().type(BLOG_ID);
cy.get('#blog-create').click();
cy.get('#blog-is-created').within(() => { cy.contains(SUCCESS_MARK) });
});

it('creates a post', () => {
cy.get('#post-id-input').clear().type(POST_ID_1);
cy.get('#post-create').click();
cy.get('#post-is-created').within(() => { cy.contains(SUCCESS_MARK) });
});

it('attaches the post to the blog', () => {
// Load the Post
cy.get('#retrieve-post-id').clear().type(POST_ID_1);
cy.get('#retrieve-post-button').click();
cy.get('#post-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-post').contains(POST_ID_1);

// Go to Edit State, Update, and Save
cy.get('#retrieved-post').within(() => {
cy.contains('Edit').click();
cy.get('#update-blogPostsId-input').clear().type(BLOG_ID);
cy.get('#update-post').click();
cy.get('#post-is-updated').contains(SUCCESS_MARK);
});
});

it('creates and attaches second post', () => {
// Create the post
cy.get('#post-id-input').clear().type(POST_ID_2);
cy.get('#post-create').click();
cy.get('#post-is-created').within(() => { cy.contains(SUCCESS_MARK) });

// Load the Post
cy.get('#retrieve-post-id').clear().type(POST_ID_2);
cy.get('#retrieve-post-button').click();
cy.get('#post-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-post').contains(POST_ID_2);

// Go to Edit State, Update, and Save
cy.get('#retrieved-post').within(() => {
cy.get('#update-blogPostsId-input').clear().type(BLOG_ID);
cy.get('#update-post').click();
cy.get('#post-is-updated').contains(SUCCESS_MARK);
});
});

it('can retrieve the blog with posts', () => {
cy.get('#retrieve-blog-id').clear().type(BLOG_ID);
cy.get('#retrieve-blog-button').click();
cy.get('#blog-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-blog').contains(BLOG_ID);
cy.get('#retrieved-blog').contains('created blog');
cy.get('#retrieved-blog').contains(POST_ID_1);
cy.get('#retrieved-blog').contains(POST_ID_2);
cy.get('#retrieved-blog').contains('created post');
});

it('can retrieve the post with blog', () => {
cy.get('#retrieve-post-id').clear().type(POST_ID_1);
cy.get('#retrieve-post-button').click();
cy.get('#post-is-retrieved').within(() => { cy.contains(SUCCESS_MARK) });
cy.get('#retrieved-post').within(() => {
cy.contains('View').click();
});
cy.get('#retrieved-post').contains(POST_ID_1);
cy.get('#retrieved-post').contains('created post');
cy.get('#retrieved-post').contains(BLOG_ID);
cy.get('#retrieved-post').contains('created blog');
});
});
});
});
Loading