|
1 | 1 | /// <reference types="Cypress" /> |
2 | | -describe('View Page', () => { |
3 | | - it('can view a short link', () => { |
| 2 | +describe("View Page", () => { |
| 3 | + it("can view a short link", () => { |
4 | 4 | const baseUrl = new URL(Cypress.config().baseUrl ?? "https://localhost:5173"); |
5 | 5 |
|
6 | 6 | // first, create a new short link |
7 | 7 | cy.request({ |
8 | | - method: 'POST', |
9 | | - url: 'http://localhost:8787/api/shorten', |
| 8 | + method: "POST", |
| 9 | + url: "http://localhost:8787/api/shorten", |
10 | 10 | body: { |
11 | | - url: new URL("/testing123", baseUrl).toString() |
12 | | - } |
| 11 | + url: new URL("/testing123", baseUrl).toString(), |
| 12 | + }, |
13 | 13 | }).then((res) => { |
14 | 14 | // then visit it |
15 | 15 | cy.visit(`/${res.body.id}`); |
16 | 16 |
|
17 | 17 | // check the page title is equal to a pretty url |
18 | | - cy.get('.mantine-Container-root > .mantine-Title-root').should('have.text', `${baseUrl.hostname}${baseUrl.port ? ":" + baseUrl.port : ""}/testing123`); |
| 18 | + cy.get(".mantine-Container-root > .mantine-Title-root").should( |
| 19 | + "have.text", |
| 20 | + `${baseUrl.hostname}${baseUrl.port ? `:${baseUrl.port}` : ""}/testing123`, |
| 21 | + ); |
19 | 22 |
|
20 | 23 | // check we have redirected to the right place |
21 | | - cy.location('href', { |
22 | | - timeout: 8000 |
23 | | - }).should('eq', new URL("/testing123", baseUrl).toString()); |
| 24 | + cy.location("href", { |
| 25 | + timeout: 8000, |
| 26 | + }).should("eq", new URL("/testing123", baseUrl).toString()); |
24 | 27 | }); |
25 | 28 | }); |
26 | 29 |
|
27 | | - it('can view a paste', () => { |
28 | | - cy.fixture('paste').then((code) => { |
| 30 | + it("can view a paste", () => { |
| 31 | + cy.fixture("paste").then((code) => { |
29 | 32 | // first, create a new paste |
30 | 33 | cy.request({ |
31 | | - method: 'POST', |
32 | | - url: 'http://localhost:8787/api/paste', |
| 34 | + method: "POST", |
| 35 | + url: "http://localhost:8787/api/paste", |
33 | 36 | body: { |
34 | | - language: 'python', |
| 37 | + language: "python", |
35 | 38 | code: code, |
36 | | - } |
| 39 | + }, |
37 | 40 | }).then((res) => { |
38 | 41 | // then visit it |
39 | 42 | const id = res.body.id; |
40 | 43 | cy.visit(`/${id}`); |
41 | 44 |
|
42 | 45 | // check the page title is expected for a paste |
43 | | - cy.get('.mantine-Container-root > .mantine-Title-root').should('have.text', 'Paste'); |
| 46 | + cy.get(".mantine-Container-root > .mantine-Title-root").should("have.text", "Paste"); |
44 | 47 | // check the content of the code box matches |
45 | | - cy.get('#paste-content pre').invoke('text').then((text) => text.replace(/\n/g, '')).should('equal', code.replace(/\n/g, '')); |
| 48 | + cy.get("#paste-content pre") |
| 49 | + .invoke("text") |
| 50 | + .then((text) => text.replace(/\n/g, "")) |
| 51 | + .should("equal", code.replace(/\n/g, "")); |
46 | 52 |
|
47 | 53 | // simulate clicking on the download link |
48 | | - cy.contains('Download').invoke('attr', 'href').then((downloadLink) => { |
49 | | - cy.request('GET', downloadLink).should((res) => { |
50 | | - expect(res.status).to.equal(200); |
51 | | - expect(res.body).to.equal(code); |
52 | | - // check the file name of the file would equal this |
53 | | - expect(res.headers["content-disposition"]).to.include(`filename="vh7-paste-${id}.txt"`); |
| 54 | + cy.contains("Download") |
| 55 | + .invoke("attr", "href") |
| 56 | + .then((downloadLink) => { |
| 57 | + cy.request("GET", downloadLink).should((res) => { |
| 58 | + expect(res.status).to.equal(200); |
| 59 | + expect(res.body).to.equal(code); |
| 60 | + // check the file name of the file would equal this |
| 61 | + expect(res.headers["content-disposition"]).to.include(`filename="vh7-paste-${id}.txt"`); |
| 62 | + }); |
54 | 63 | }); |
55 | | - }); |
56 | 64 | }); |
57 | 65 | }); |
58 | 66 | }); |
59 | 67 |
|
60 | | - it('can view an upload', () => { |
| 68 | + it("can view an upload", () => { |
61 | 69 | // the window object is needed for making a form request with a file |
62 | 70 | cy.window().then((_win) => { |
63 | | - cy.fixture('image.png', "binary").then((image) => { |
64 | | - const imageBlob = Cypress.Blob.binaryStringToBlob(image, 'image/png'); |
| 71 | + cy.fixture("image.png", "binary").then((image) => { |
| 72 | + const imageBlob = Cypress.Blob.binaryStringToBlob(image, "image/png"); |
65 | 73 |
|
66 | 74 | // create a new formdata object with the file |
67 | 75 | const data = new FormData(); |
68 | | - data.set('file', imageBlob, 'image.png'); |
| 76 | + data.set("file", imageBlob, "image.png"); |
69 | 77 |
|
70 | 78 | cy.request({ |
71 | | - method: 'POST', |
72 | | - url: 'http://localhost:8787/api/upload', |
| 79 | + method: "POST", |
| 80 | + url: "http://localhost:8787/api/upload", |
73 | 81 | body: data, |
74 | 82 | headers: { |
75 | | - "Content-Type": "multipart/form-data" |
76 | | - } |
| 83 | + "Content-Type": "multipart/form-data", |
| 84 | + }, |
77 | 85 | }).then((res) => { |
78 | 86 | // then visit it |
79 | 87 | const json = JSON.parse(new TextDecoder().decode(res.body)); |
80 | 88 | const id = json.id; |
81 | 89 | cy.visit(`/${id}`); |
82 | 90 |
|
83 | 91 | // check the title of the page matches the uploaded filename |
84 | | - cy.get('.mantine-Container-root > .mantine-Title-root').should('have.text', 'image.png'); |
| 92 | + cy.get(".mantine-Container-root > .mantine-Title-root").should("have.text", "image.png"); |
85 | 93 | // check the hash is correct |
86 | | - cy.get('#upload-sha256').should('have.text', '3f9dba53159de1ec89b2a9a0821c3fd0290e3c5566e31daf967aaa8d05b63e35'); |
| 94 | + cy.get("#upload-sha256").should( |
| 95 | + "have.text", |
| 96 | + "3f9dba53159de1ec89b2a9a0821c3fd0290e3c5566e31daf967aaa8d05b63e35", |
| 97 | + ); |
87 | 98 |
|
88 | 99 | // simulate clicking the download link |
89 | | - cy.contains('Download image.png').invoke('attr', 'href').then((downloadLink) => { |
90 | | - cy.request({ |
91 | | - method: 'GET', |
92 | | - url: downloadLink, |
93 | | - encoding: 'base64' |
94 | | - }).should((res_1) => { |
95 | | - expect(res_1.status).to.equal(200); |
96 | | - Cypress.Blob.blobToBase64String(imageBlob).then((b64String) => { |
97 | | - expect(res_1.body).to.equal(b64String); |
98 | | - }); |
| 100 | + cy.contains("Download image.png") |
| 101 | + .invoke("attr", "href") |
| 102 | + .then((downloadLink) => { |
| 103 | + cy.request({ |
| 104 | + method: "GET", |
| 105 | + url: downloadLink, |
| 106 | + encoding: "base64", |
| 107 | + }).should((res_1) => { |
| 108 | + expect(res_1.status).to.equal(200); |
| 109 | + Cypress.Blob.blobToBase64String(imageBlob).then((b64String) => { |
| 110 | + expect(res_1.body).to.equal(b64String); |
| 111 | + }); |
99 | 112 |
|
100 | | - // check the name of the downloaded file is the same |
101 | | - expect(res_1.headers["content-disposition"]).to.include(`filename="image.png"`); |
| 113 | + // check the name of the downloaded file is the same |
| 114 | + expect(res_1.headers["content-disposition"]).to.include(`filename="image.png"`); |
| 115 | + }); |
102 | 116 | }); |
103 | | - }); |
104 | 117 | }); |
105 | 118 | }); |
106 | 119 | }); |
|
0 commit comments