Skip to content

Commit 1f0b742

Browse files
committed
completed test coverage
1 parent c3e81e9 commit 1f0b742

File tree

7 files changed

+77
-203
lines changed

7 files changed

+77
-203
lines changed

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@testing-library/jest-dom": "^5.11.4",
88
"@testing-library/react": "^11.1.0",
99
"@testing-library/user-event": "^12.1.10",
10+
"jest-cli": "^26.6.3",
1011
"react": "^17.0.2",
1112
"react-dom": "^17.0.2",
1213
"react-scripts": "4.0.3",
@@ -18,6 +19,7 @@
1819
"test": "react-scripts test",
1920
"test:update": "react-scripts test --updateSnapshot --watchAll=false",
2021
"test:coverage": "react-scripts test --coverage",
22+
"nyc": "nyc mocha",
2123
"eject": "react-scripts eject"
2224
},
2325
"eslintConfig": {
@@ -38,6 +40,13 @@
3840
"last 1 safari version"
3941
]
4042
},
43+
"jest": {
44+
"collectCoverageFrom": [
45+
"src/**/*.{js,jsx}",
46+
"!src/index.js",
47+
"!src/reportWebVitals.js"
48+
]
49+
},
4150
"devDependencies": {
4251
"react-test-renderer": "^17.0.2"
4352
}

src/App.js

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
Author: Sergio J Falcon
3-
Date: March 22nd, 2021
3+
Date: May 6th, 2021
44
Course: CS5103 - Software Engineering
55
Project: Word Statistics
66
**/
@@ -29,7 +29,7 @@ function App() {
2929
const reader = new FileReader();
3030
let file = e.target.files[0];
3131
setFileName(file.name);
32-
32+
3333
reader.readAsText(e.target.files[0]);
3434

3535
reader.onload = function () {
@@ -52,55 +52,56 @@ function App() {
5252
<div className="header">
5353
<h4>Created by: Sergio J Falcon</h4>
5454
</div>
55-
{replaceWord('one one two three', 'one,', 'first')}
5655

5756
{!fileName ? <h1>Upload a file to run Word Statistics</h1>
58-
: <h1>File uploaded: {fileName}</h1>}
57+
: <h1 data-testid="file-name">File uploaded: {fileName}</h1>}
5958
<div className="container">
6059
<div className="button-wrap">
6160
<label className="button" htmlFor="uploadFile">Upload File</label>
62-
<input id="uploadFile" type="file" onChange={(e) => handleFiles(e, 0)}></input>
61+
<input id="uploadFile" type="file" data-testid="upload-input" onChange={(e) => handleFiles(e, 0)}></input>
6362
</div>
6463
</div>
65-
<div className='text'>
64+
<div className='text' data-testid="file-text">
6665
{text ? printCleanList(cleanList(text)) : <p>empty</p>}
6766
</div>
6867
<div className="function_container">
6968
<div className="column">
70-
<h2 title="word-count">Word Count: </h2><p>{wordCount(text)}</p>
69+
<h2 title="word-count">Word Count: </h2>
70+
<p>{wordCount(text)}</p>
7171
</div>
7272
<div className="column">
7373
<h2>Word Frequency: </h2>
7474
{text ? (wordFrequency(text).map(entry =>(
75-
<p>{entry.word}: {entry.counter}</p>
75+
<p key={entry.word}>{entry.word}: {entry.counter}</p>
7676
))) : null}
7777
</div>
7878
<div className="column">
7979
<h2>Line Count: </h2>
8080
<p>{lineCount(text)}</p>
8181
</div>
8282
<div className="column">
83-
<h2>Char Count: </h2><p>{charCount(text)}</p>
83+
<h2>Char Count: </h2>
84+
<p>{charCount(text)}</p>
8485
</div>
8586
<div className="column">
8687
<h2>Unique Char Frequency: </h2>
8788
{text ? (uniqueCharFrequency(text).map(entry => (
88-
<p>{entry.character}: {entry.counter}</p>
89+
<p key={entry.character}>{entry.character}: {entry.counter}</p>
8990
))) : null}
9091
</div>
9192
<div className="column">
9293
<h2>Word Replacement: </h2>
93-
<form onSubmit={handleSubmit}>
94+
<form id="form-id" onSubmit={handleSubmit} data-testid="form">
9495
<div>
9596
<label>Word to be replaced: </label>
96-
<input type="text" {...bindPreviousWord} />
97+
<input type="text" data-testid="word-before-input" {...bindPreviousWord} />
9798
</div>
9899

99100
<div>
100101
<label>Replacement for said word:</label>
101-
<input type="text" {...bindReplacementWord} />
102+
<input type="text" data-testid="new-word" {...bindReplacementWord} />
102103
</div>
103-
<input type="submit" value="Submit" />
104+
<button form="form-id" type="submit" data-testid="enter-input">enter</button>
104105
</form>
105106
<p>{text}</p>
106107
</div>

src/App.test.js

+52-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,60 @@
1-
import { fireEvent, render, screen } from '@testing-library/react';
1+
import { fireEvent, getByTestId, render, screen, waitFor, debug } from '@testing-library/react';
22
import App from './App';
3-
import { create } from 'react-test-renderer'
43

5-
describe('My first snapshot test', ()=> {
6-
test('testing app', () => {
7-
let tree = create(<App />);
8-
expect(tree.toJSON()).toMatchSnapshot();
9-
})
10-
});
11-
describe('Inputting a text file', () => {
12-
// test('input textfile', () => {
13-
// // const component = render(<App />);
14-
15-
// // fireEvent.click(screen.getByText('Upload File'));
16-
// });
17-
test('handleFiles', () => {
18-
4+
describe('App', () => {
5+
6+
test('renders App Component', async () => {
7+
render(<App />);
8+
expect(screen.getByText(/Upload a file to run Word Statistics/)).toBeInTheDocument();
9+
expect(screen.getByText(/empty/)).toBeInTheDocument();
10+
expect(screen.getByText(/Word Count:/)).toBeInTheDocument();
11+
expect(screen.getByText(/Word Frequency:/)).toBeInTheDocument();
12+
expect(screen.getByText(/Line Count:/)).toBeInTheDocument();
13+
expect(screen.getByText(/Char Count:/)).toBeInTheDocument();
14+
expect(screen.getByText(/Unique Char Frequency:/)).toBeInTheDocument();
15+
expect(screen.getByText(/Word Replacement:/)).toBeInTheDocument();
16+
expect(screen.getByText(/Word to be replaced:/)).toBeInTheDocument();
17+
expect(screen.getByText(/Replacement for said word:/)).toBeInTheDocument();
1918
});
20-
test('handleSubmit', () => {
2119

20+
test('handleFiles', async () => {
21+
let app = render(<App />);
22+
23+
const file = new File(['a bb ccc'], 'data.txt', {type: "text/plain;charset=utf-8"});
24+
const handleFileUploadMockFn = jest.fn();
25+
26+
await waitFor(() =>
27+
fireEvent.change(app.getByTestId('upload-input'), {
28+
target: {files: [file]},
29+
}));
30+
31+
let textfile = app.getAllByTestId('file-name').have
32+
expect(screen.getByText(/File uploaded: data.txt/)).toBeInTheDocument();
33+
34+
await waitFor(() => {
35+
expect(handleFileUploadMockFn).not.toHaveBeenCalled();
36+
}
37+
);
2238
});
23-
test('has correct body', () => {
24-
render(<App />);
25-
let headertwo = [<h2 title="word-count">Word Count: </h2>, <h2>Word Frequency: </h2>, <h2>Line Count: </h2>, <h2>Char Count: </h2>, <h2>Unique Char Frequency: </h2>, <h2>Word Replacement: </h2>]
26-
// let wordCountHeader = <h2 t)
27-
// expect(screen.getByRole('heading', { level: 4 })).toHaveTextContent('Created by: Sergio J Falcon');
28-
// expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent('Upload a file to run Word Statistics');
29-
// expect(screen.queryAllByRole('heading', { level: 2 })).toHaveTextContent(headertwo);
30-
// console.log(getByText('Word Count: ', 'h2'))
31-
//expect(screen.getAllByTitle('word-count')).toHaveTextContent('Word Count: ');
39+
test('wrong file for fileReader', () => {
40+
let app = render(<App />);
41+
const file = new File([], '');
42+
const mTxt = new Blob(["text inside this file"], {type: "text/plain;charset=utf-8"})
43+
44+
const mEvent = { target: {files: [file]}};
3245

3346
})
47+
test('handleSubmit', () => {
48+
const mockSubmit = jest.fn(() => {
49+
console.log("onSubmit");
50+
});
51+
const {getByTestId } = render(<App handleSubmit={mockSubmit} />);
52+
53+
fireEvent.change(getByTestId("word-before-input"), { target: {value: 'a' }});
54+
fireEvent.change(getByTestId("new-word", { target: { value: 'one'}}));
55+
fireEvent.click(getByTestId("enter-input"));
56+
fireEvent.submit(getByTestId("form"));
57+
58+
expect(mockSubmit).not.toHaveBeenCalled();
59+
});
3460
});

src/FileInput.js

-30
This file was deleted.

src/__snapshots__/App.test.js.snap

-127
This file was deleted.

src/manipulation.test.js

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { render, screen } from '@testing-library/react'
33

44
import {printCleanList, printMap, cleanList, wordCount, wordFrequency, lineCount, charCount, uniqueCharFrequency, replaceWord} from './manipulation';
55

6-
7-
test('has correct welcome text', () => {
8-
render(<h4>Created By: Sergio J Falcon</h4>)
9-
expect(screen.getByRole('heading')).toHaveTextContent('Created By: Sergio J Falcon')
10-
})
116
describe('String Manipulation', ()=> {
127
test('empty data file', () => {
138

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -6326,7 +6326,7 @@ [email protected]:
63266326
stack-utils "^2.0.2"
63276327
throat "^5.0.0"
63286328

6329-
jest-cli@^26.6.0:
6329+
jest-cli@^26.6.0, jest-cli@^26.6.3:
63306330
version "26.6.3"
63316331
resolved "https://registry.yarnpkg.com/jest-cli/-/jest-cli-26.6.3.tgz#43117cfef24bc4cd691a174a8796a532e135e92a"
63326332
integrity sha512-GF9noBSa9t08pSyl3CY4frMrqp+aQXFGFkf5hEPbh/pIUFYWMK6ZLTfbmadxJVcJrdRoChlWQsA2VkJcDFK8hg==

0 commit comments

Comments
 (0)