Skip to content

Commit 6b95a04

Browse files
committed
Adding SearchBar component (tests passing)
1 parent 5802e53 commit 6b95a04

File tree

7 files changed

+110
-9
lines changed

7 files changed

+110
-9
lines changed

cypress/support/component-index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6-
<meta name="viewport" content="width=device-width,initial-scale=1.0">
4+
<meta charset="utf-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
77
<title>Components App</title>
88
</head>
99
<body>
1010
<div data-cy-root></div>
1111
</body>
12-
</html>
12+
</html>

cypress/support/component.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
// ***********************************************************
1515

1616
// Import commands.js using ES2015 syntax:
17-
import './commands'
17+
import "./commands";
1818

1919
// Alternatively you can use CommonJS syntax:
2020
// require('./commands')
2121

22-
import { mount } from 'cypress/vue'
22+
import { mount } from "cypress/vue";
2323

24-
Cypress.Commands.add('mount', mount)
24+
Cypress.Commands.add("mount", mount);
2525

2626
// Example use:
27-
// cy.mount(MyComponent)
27+
// cy.mount(MyComponent)

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"devDependencies": {
1616
"@babel/core": "^7.12.16",
1717
"@babel/eslint-parser": "^7.12.16",
18+
"@cypress/vue": "^5.0.5",
1819
"@vue/cli-plugin-babel": "~5.0.0",
1920
"@vue/cli-plugin-eslint": "~5.0.0",
2021
"@vue/cli-service": "~5.0.0",

src/components/HomePage.vue

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<div class="home-page">
33
<h1>Stocks</h1>
4+
<search-bar :value="searchQuery" @update="searchQuery = $event" />
45
<table>
56
<thead>
67
<tr>
@@ -11,7 +12,7 @@
1112
</tr>
1213
</thead>
1314
<tbody>
14-
<tr v-for="stock in stocks" :key="stock.ticker">
15+
<tr v-for="stock in filteredStocks" :key="stock.ticker">
1516
<td>{{ stock.name }}</td>
1617
<td>{{ stock.ticker }}</td>
1718
<td>{{ stock.price }}</td>
@@ -23,9 +24,15 @@
2324
</template>
2425

2526
<script>
27+
import SearchBar from "../components/SearchBar.vue";
28+
2629
export default {
30+
components: {
31+
SearchBar,
32+
},
2733
data() {
2834
return {
35+
searchQuery: "",
2936
stocks: [
3037
{ name: "Apple Inc.", ticker: "AAPL", price: 135.75, change: -0.25 },
3138
{
@@ -50,6 +57,17 @@ export default {
5057
],
5158
};
5259
},
60+
computed: {
61+
filteredStocks() {
62+
return this.stocks.filter((stock) => {
63+
const query = this.searchQuery.toLowerCase();
64+
return (
65+
stock.name.toLowerCase().includes(query) ||
66+
stock.ticker.toLowerCase().includes(query)
67+
);
68+
});
69+
},
70+
},
5371
};
5472
</script>
5573

src/components/SearchBar.vue

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<template>
2+
<div class="search-bar">
3+
<input
4+
type="text"
5+
:value="searchQuery"
6+
@input="$emit('update', $event.target.value)"
7+
placeholder="Search"
8+
/>
9+
</div>
10+
</template>
11+
<script>
12+
export default {
13+
props: {
14+
value: {
15+
type: String,
16+
default: "",
17+
},
18+
placeholder: {
19+
type: String,
20+
default: "Search",
21+
},
22+
},
23+
data() {
24+
return {
25+
searchQuery: this.value,
26+
};
27+
},
28+
watch: {
29+
value(newVal) {
30+
this.searchQuery = newVal;
31+
},
32+
},
33+
};
34+
</script>
35+
36+
<style scoped>
37+
.search-bar {
38+
margin-bottom: 1rem;
39+
}
40+
</style>
41+
42+
<style scoped>
43+
.search-bar {
44+
margin-bottom: 1rem;
45+
}
46+
</style>

tests/SearchBar.cy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SearchBar from "../src/components/SearchBar";
2+
3+
describe("SearchBar", () => {
4+
beforeEach(() => {
5+
cy.mount(SearchBar);
6+
});
7+
8+
it("renders a search input bar with given placeholder", () => {
9+
cy.get(".search-bar input").should("have.attr", "placeholder", "Search");
10+
});
11+
12+
it("updates input value with user input", () => {
13+
const inputText = "test search query";
14+
cy.get(".search-bar input").type(inputText).should("have.value", inputText);
15+
});
16+
});

0 commit comments

Comments
 (0)