Skip to content

Commit 0116624

Browse files
authored
feat(validation): UI for interacting with /api/validate endpoint (#5)
* - Added CSS file rules for editorconfig (2-indent) - Made background a little lighter * Added main content container to root layout * Initial UI for interacting with the validate API endpoint * Added htmlFor to loading textarea to fix build * Turn off the 'react/display-name' linting rule to allow for loading components via a .Loading attribute * Fix bottom padding of option label * Added empty default string values to additional classname parameters to avoid undefined classnames * Install Cypress * Initial workflow for E2E testing for the preview branch * Specify baseUrl via config instead of env var * - Update setup-go action version 4 -> 5 - Use cache in initial dependency installation job - Don't install in final e2e testing stage - Add install-deps as a dependency of the test-preview stage * - Remove lookup-only from initial dep cache - Add Cypress binary to cache * Run E2E test against per-deployment URLs (allows for concurrent/cross-branch execution of e2e tests) * Fix saving preview URL to GitHub output * Add Cypress component config * Initial component tests * Add component tests to CI * Don't deploy unless component tests pass * Add required testId to TextArea component * E2E test * - Use beforeEach in component test to enforce DRY - Change wording of "show excluded" to "show ignored" - Added data-cy tags to options area and submit button * - Added new testing commands - Updated docs for testing
1 parent 18ab30a commit 0116624

20 files changed

Lines changed: 2297 additions & 174 deletions

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ charset = utf-8
1111
trim_trailing_whitespace = false
1212
insert_final_newline = false
1313

14-
[*.yaml]
14+
[*.{yaml,css,config.ts}]
1515
indent_size = 2

.eslintrc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"extends": "next/core-web-vitals"
2+
"extends": "next/core-web-vitals",
3+
"rules": {
4+
"react/display-name": "off"
5+
}
36
}

.github/workflows/vercel-preview.yaml

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,55 @@ on:
1111
- main
1212

1313
jobs:
14+
install-deps:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout repo
18+
uses: actions/checkout@v4
19+
20+
- name: Cache dependencies
21+
id: cache
22+
uses: actions/cache@v4
23+
with:
24+
path: |
25+
node_modules
26+
~/.npm
27+
~/.cache/Cypress
28+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
29+
30+
- if: steps.cache.outputs.cache-hit != 'true'
31+
name: Install dependencies
32+
run: npm ci
33+
34+
- if: steps.cache.outputs.cache-hit == 'true'
35+
name: Link cached dependencies
36+
run: npm install
37+
38+
test-components:
39+
needs: install-deps
40+
runs-on: ubuntu-latest
41+
steps:
42+
- name: Checkout repo
43+
uses: actions/checkout@v4
44+
45+
- name: Restore dependency cache
46+
uses: actions/cache@v4
47+
with:
48+
path: |
49+
node_modules
50+
~/.npm
51+
~/.cache/Cypress
52+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
53+
54+
- name: Link cached dependencies
55+
run: npm install
56+
57+
- name: Run component tests
58+
uses: cypress-io/github-action@v6
59+
with:
60+
install: false
61+
component: true
62+
1463
test-api:
1564
runs-on: ubuntu-latest
1665
steps:
@@ -20,7 +69,7 @@ jobs:
2069
api/validate
2170
2271
- name: Set up Go
23-
uses: actions/setup-go@v4
72+
uses: actions/setup-go@v5
2473
with:
2574
go-version: "1.18"
2675

@@ -35,7 +84,9 @@ jobs:
3584
go test -v
3685
3786
deploy-preview:
38-
needs: test-api
87+
needs: [test-api, test-components]
88+
outputs:
89+
preview-url: ${{ steps.set-preview-url.outputs.preview-url }}
3990
runs-on: ubuntu-latest
4091
steps:
4192
- uses: actions/checkout@v4
@@ -46,4 +97,33 @@ jobs:
4697
run: vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }}
4798

4899
- name: Deploy directly to Vercel
49-
run: vercel deploy --token=${{ secrets.VERCEL_TOKEN }}
100+
run: vercel deploy --token=${{ secrets.VERCEL_TOKEN }} > preview-url.txt
101+
102+
- name: Set Preview URL
103+
id: set-preview-url
104+
run: echo "preview-url=$(cat preview-url.txt)" >> "$GITHUB_OUTPUT"
105+
106+
test-preview:
107+
needs: [deploy-preview, install-deps]
108+
runs-on: ubuntu-latest
109+
steps:
110+
- name: Checkout repo
111+
uses: actions/checkout@v4
112+
113+
- name: Restore dependency cache
114+
uses: actions/cache@v4
115+
with:
116+
path: |
117+
node_modules
118+
~/.npm
119+
~/.cache/Cypress
120+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
121+
122+
- name: Link cached dependencies
123+
run: npm install
124+
125+
- name: Run E2E tests
126+
uses: cypress-io/github-action@v6
127+
with:
128+
config: baseUrl=${{ needs.deploy-preview.outputs.preview-url }}
129+
install: false

README.md

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,102 @@
22

33
- [About](#about)
44
- [Feature list](#feature-list)
5+
- [Local Development](#local-development)
6+
- [Testing](#testing)
7+
- [Adding new tests](#adding-new-tests)
58
- [API Endpoints](#api-endpoints)
69
- [`/api/validate`](#apivalidate)
710

8-
# About
11+
## About
912

1013
A tool to verify your `.dockerignore` file configuration.
1114

12-
## Features
15+
### Features
1316

14-
- [ ] Visual interface
17+
- [x] Visual interface
1518
- [ ] Syntax guide for writing a well-formed `.dockerignore` file
16-
- [ ] Input your `.dockerignore` configuration to validate its syntax
19+
- [x] Input your `.dockerignore` configuration to validate its syntax
1720
- [ ] Highlight any syntax errors (+ duplicate lines)
18-
- [ ] Input a project/directory structure to match using the `.dockerignore`
21+
- [x] Input a project/directory structure to match using the `.dockerignore`
1922
- [ ] Display all files which will match against the config file, and those that won't
2023
- [ ] Input a GitHub URL (public for now, maybe auth in future for private repos?)
2124
- [ ] Automagically run the `.dockerignore` against the repo's structure
2225
- [x] API
2326
- [x] Allow programmatic `POST`s to a `/api/validate` endpoint
2427
- [ ] Response includes information about the validity of the `.dockerignore`
2528

26-
# API Endpoints
29+
## Local Development
30+
31+
As far as prerequisites go, you will need to have the [Vercel CLI installed](https://vercel.com/docs/cli#installing-vercel-cli).
32+
33+
To run this project locally, clone it onto your local machine.
34+
35+
```bash
36+
git clone git@github.com:DiggidyDev/dockerignore-validator.git
37+
```
38+
39+
You can start a local development server with:
40+
41+
```bash
42+
vercel dev
43+
```
44+
45+
### Testing
46+
47+
To open Cypress for local testing:
48+
49+
```bash
50+
npm run test
51+
```
52+
53+
To run all e2e tests headlessly:
54+
55+
```bash
56+
npm run test:e2e
57+
```
58+
59+
###### Ensure your development server is running for the e2e tests to run
60+
61+
To run all component tests headlessly:
62+
63+
```bash
64+
npm run test:cmp
65+
```
66+
67+
###### No development server is required for these tests to run
68+
69+
### Adding new tests
70+
71+
If you wish to add a new e2e test, it should live under:
72+
73+
```
74+
cypress/
75+
└── e2e/
76+
└── TestName.cy.ts
77+
```
78+
79+
##### Tree diagram generated courtesy of [Nathan Friend's Tree tool](https://tree.nathanfriend.io/)
80+
81+
If you wish to add a new component test, it should live under:
82+
83+
```
84+
src/
85+
└── app/
86+
└── components/
87+
└── ComponentName/
88+
├── ComponentName.cy.tsx
89+
└── ComponentName.tsx
90+
```
91+
92+
##### Tree diagram generated courtesy of [Nathan Friend's Tree tool](https://tree.nathanfriend.io/)
93+
94+
## API Endpoints
2795

2896
When interacting with the API, no validation is required as of now.
2997

3098
It's important to note that each **Response example** is what would be returned from the endpoint as though the **Request Body** was provided.
3199

32-
## `/api/validate`
100+
### `/api/validate`
33101

34102
- **Description**: Validates filepaths against a .dockerignore configuration.
35103
- **Method**: `POST`

cypress.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { defineConfig } from "cypress";
2+
3+
export default defineConfig({
4+
e2e: {
5+
baseUrl: "http://localhost:3000",
6+
setupNodeEvents(on, config) {
7+
// implement node event listeners here
8+
},
9+
},
10+
11+
component: {
12+
devServer: {
13+
framework: "next",
14+
bundler: "webpack",
15+
},
16+
},
17+
});

cypress/e2e/validate.cy.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
describe("Homepage", () => {
2+
beforeEach(() => {
3+
cy.visit("/");
4+
});
5+
6+
it("should have all inputs present and in the correct initial state", () => {
7+
// Test inputs are present and valid
8+
cy.get("[data-cy=dockerignore-input] > textarea")
9+
.should("exist")
10+
.and("be.enabled");
11+
cy.get("[data-cy=dockerignore-input] > label")
12+
.should("exist")
13+
.and("have.text", ".dockerignore");
14+
15+
cy.get("[data-cy=files-input] > textarea")
16+
.should("exist")
17+
.and("be.enabled");
18+
cy.get("[data-cy=files-input] > label")
19+
.should("exist")
20+
.and("have.text", "Files");
21+
22+
cy.get("[data-cy=options] > input[name=showIgnored]")
23+
.should("exist")
24+
.and("be.checked");
25+
26+
cy.get("[data-cy=validate-button]").should("exist").and("be.enabled");
27+
28+
// Test output is present and in the correct state
29+
cy.get("[data-cy=result-output] > textarea")
30+
.should("exist")
31+
.and("be.disabled");
32+
cy.get("[data-cy=result-output] > label")
33+
.should("exist")
34+
.and("have.text", "Ignored Files");
35+
});
36+
37+
it("should allow form submission with correct ", () => {
38+
const dockerignore = "node_modules";
39+
const files = "node_modules\npackage.json";
40+
41+
// Output text with "Show ignored files" checked
42+
const expectedIgnoredOutput = "node_modules";
43+
// Output text with "Show ignored files" unchecked
44+
const expectedCopyFilesOutput = "package.json";
45+
// API Response
46+
const expectedRes = [true, false];
47+
48+
// Ensure the correct data is being sent and received
49+
cy.intercept("POST", "/api/validate", (req) => {
50+
expect(JSON.parse(req.body)).to.deep.equal({
51+
dockerignore: ["node_modules"],
52+
files: ["node_modules", "package.json"],
53+
});
54+
req.continue((res) => {
55+
expect(res.statusCode).to.equal(200);
56+
expect(res.body).to.deep.equal(expectedRes);
57+
});
58+
}).as("validate");
59+
60+
cy.get("[data-cy=dockerignore-input] > textarea").type(dockerignore);
61+
cy.get("[data-cy=files-input] > textarea").type(files);
62+
63+
// Ensure the output is empty and the loading state is not present
64+
cy.get("[data-cy=result-output] > textarea").should("be.empty");
65+
cy.get("[data-cy=textarea-loading]").should("not.exist");
66+
67+
// Once clicked, the result should be replaced with a loading state
68+
cy.get("[data-cy=validate-button]").click();
69+
cy.get("[data-cy=result-output]").should("not.exist");
70+
cy.get("[data-cy=textarea-loading]").should("exist");
71+
cy.get("[data-cy=textarea-loading] > label").should(
72+
"have.text",
73+
"Matching patterns..."
74+
);
75+
76+
cy.wait("@validate");
77+
78+
// Loading state should be removed and the result should be present
79+
cy.get("[data-cy=textarea-loading]").should("not.exist");
80+
cy.get("[data-cy=result-output]").should("exist");
81+
82+
// Ensure the result has the correct output for both checked
83+
// and unchecked states of "Show ignored files"
84+
cy.get("[data-cy=result-output] > textarea").should(
85+
"have.text",
86+
expectedIgnoredOutput
87+
);
88+
89+
cy.get("[data-cy=options] > input[name=showIgnored]").uncheck();
90+
cy.get("[data-cy=result-output] > label").should(
91+
"have.text",
92+
"Files to Copy"
93+
);
94+
cy.get("[data-cy=result-output] > textarea").should(
95+
"have.text",
96+
expectedCopyFilesOutput
97+
);
98+
});
99+
});

cypress/support/commands.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************
3+
// This example commands.ts shows you how to
4+
// create various custom commands and overwrite
5+
// existing commands.
6+
//
7+
// For more comprehensive examples of custom
8+
// commands please read more here:
9+
// https://on.cypress.io/custom-commands
10+
// ***********************************************
11+
//
12+
//
13+
// -- This is a parent command --
14+
// Cypress.Commands.add('login', (email, password) => { ... })
15+
//
16+
//
17+
// -- This is a child command --
18+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
19+
//
20+
//
21+
// -- This is a dual command --
22+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
23+
//
24+
//
25+
// -- This will overwrite an existing command --
26+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
27+
//
28+
// declare global {
29+
// namespace Cypress {
30+
// interface Chainable {
31+
// login(email: string, password: string): Chainable<void>
32+
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
33+
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
34+
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
35+
// }
36+
// }
37+
// }

0 commit comments

Comments
 (0)