Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
react-rails-rspec
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Aman Sharma
react-rails-rspec
Commits
d41bb425
Commit
d41bb425
authored
Oct 03, 2020
by
Abhisarika Singh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
worked on model using factories,api, controller & features testcases
parent
87e27025
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
163 additions
and
21 deletions
+163
-21
books_controller.rb
app/controllers/api/v1/books_controller.rb
+0
-1
AddBooks.jsx
app/javascript/components/library/AddBooks.jsx
+11
-11
BookDetails.jsx
app/javascript/components/library/BookDetails.jsx
+33
-1
BooksList.jsx
app/javascript/components/library/BooksList.jsx
+5
-2
Library.jsx
app/javascript/components/library/Library.jsx
+5
-1
book.rb
spec/factories/book.rb
+8
-0
homepage._spec.rb
spec/features/homepage._spec.rb
+2
-0
library_spec.rb
spec/features/library_spec.rb
+13
-0
book_spec.rb
spec/models/book_spec.rb
+58
-1
rails_helper.rb
spec/rails_helper.rb
+1
-0
book_request_spec.rb
spec/requests/api/v1/book_request_spec.rb
+27
-4
No files found.
app/controllers/api/v1/books_controller.rb
View file @
d41bb425
...
@@ -25,7 +25,6 @@ class Api::V1::BooksController < ApplicationController
...
@@ -25,7 +25,6 @@ class Api::V1::BooksController < ApplicationController
def
destroy
def
destroy
puts
"hhhgh"
book
&
.
destroy
book
&
.
destroy
render
json:
{
message:
'Book deleted!'
}
render
json:
{
message:
'Book deleted!'
}
end
end
...
...
app/javascript/components/library/AddBooks.jsx
View file @
d41bb425
...
@@ -17,7 +17,7 @@ class AddBooks extends Component {
...
@@ -17,7 +17,7 @@ class AddBooks extends Component {
}
}
goBack
()
{
goBack
()
{
Router
.
history
.
goBack
(
);
this
.
props
.
history
.
go
(
-
1
);
}
}
saveBook
(
event
)
{
saveBook
(
event
)
{
...
@@ -43,14 +43,14 @@ class AddBooks extends Component {
...
@@ -43,14 +43,14 @@ class AddBooks extends Component {
},
},
body
:
JSON
.
stringify
(
body
)
body
:
JSON
.
stringify
(
body
)
})
})
.
then
(
response
=>
{
.
then
(
response
=>
{
if
(
response
.
ok
)
{
if
(
response
.
ok
)
{
return
response
.
json
();
return
response
.
json
();
}
}
throw
new
Error
(
"Network response was not ok."
);
throw
new
Error
(
"Network response was not ok."
);
})
})
.
then
(
response
=>
this
.
props
.
history
.
push
(
'/booksList'
))
.
then
(
response
=>
this
.
props
.
history
.
push
(
'/booksList'
))
.
catch
(
error
=>
console
.
log
(
error
.
message
));
.
catch
(
error
=>
console
.
log
(
error
.
message
));
}
}
getFieldValue
(
event
)
{
getFieldValue
(
event
)
{
...
@@ -108,10 +108,10 @@ class AddBooks extends Component {
...
@@ -108,10 +108,10 @@ class AddBooks extends Component {
</
form
>
</
form
>
</
div
>
</
div
>
<
button
onClick=
{
this
.
goBack
}
>
Go Back
</
button
>
<
button
onClick=
{
this
.
goBack
}
className=
"btn btn-link"
>
Go Back
</
button
>
<
Link
to=
"/"
className=
"btn btn-link"
>
<
Link
to=
"/"
className=
"btn btn-link"
>
Home
Home
</
Link
>
</
Link
>
</
main
>
</
main
>
</
div
>
</
div
>
</>)
</>)
...
...
app/javascript/components/library/BookDetails.jsx
View file @
d41bb425
...
@@ -6,6 +6,8 @@ class BookDetails extends Component{
...
@@ -6,6 +6,8 @@ class BookDetails extends Component{
super
(
props
);
super
(
props
);
this
.
setState
=
this
.
setState
.
bind
(
this
)
this
.
setState
=
this
.
setState
.
bind
(
this
)
this
.
deleteBook
=
this
.
deleteBook
.
bind
(
this
)
this
.
deleteBook
=
this
.
deleteBook
.
bind
(
this
)
this
.
updateBookDetails
=
this
.
updateBookDetails
(
this
)
this
.
getToken
=
this
.
getToken
.
bind
(
this
)
this
.
state
=
{
book
:
{}
}
this
.
state
=
{
book
:
{}
}
}
}
...
@@ -53,6 +55,32 @@ class BookDetails extends Component{
...
@@ -53,6 +55,32 @@ class BookDetails extends Component{
.
catch
(
error
=>
console
.
log
(
error
.
message
));
.
catch
(
error
=>
console
.
log
(
error
.
message
));
}
}
updateBookDetails
(){
const
id
=
this
.
props
.
location
.
search
.
split
(
'?'
)[
1
].
split
(
'='
)[
1
]
const
url
=
`/api/v1/book/update/
${
id
}
`
;
const
token
=
document
.
querySelector
(
'meta[name="csrf-token"]'
).
content
;
fetch
(
url
,
{
method
:
"GET"
,
headers
:
{
"X-CSRF-Token"
:
token
,
"Content-Type"
:
"application/json"
}
})
.
then
(
response
=>
{
if
(
response
.
ok
)
{
return
response
.
json
();
}
throw
new
Error
(
"Network response was not ok."
);
})
.
then
((
response
)
=>
this
.
setState
({
book
:
response
}))
.
catch
(
error
=>
console
.
log
(
error
.
message
));
}
getToken
(){
return
document
.
querySelector
(
'meta[name="csrf-token"]'
).
content
;
}
render
(){
render
(){
const
{
book
}
=
this
.
state
const
{
book
}
=
this
.
state
return
(
return
(
...
@@ -65,12 +93,16 @@ class BookDetails extends Component{
...
@@ -65,12 +93,16 @@ class BookDetails extends Component{
<
div
className=
"py-5"
>
<
div
className=
"py-5"
>
<
main
className=
"container"
>
<
main
className=
"container"
>
<
div
className=
"text-right mb-3"
>
<
div
className=
"text-right mb-3"
>
<
Link
to=
"/"
className=
"btn btn-link"
>
Home
</
Link
>
<
button
onClick=
{
this
.
props
.
history
.
goBack
}
className=
"btn btn-link"
></
button
>
</
div
>
</
div
>
<
div
className=
"row"
>
<
div
className=
"row"
>
<
div
>
{
book
.
name
}
</
div
>
<
div
>
{
book
.
name
}
</
div
>
<
div
>
{
book
.
writterName
}
</
div
>
<
div
>
{
book
.
writterName
}
</
div
>
<
div
>
{
book
.
courseName
}
</
div
>
<
div
>
{
book
.
courseName
}
</
div
>
<
button
>
edit
</
button
>
{
/* <button onClick={this.updateBookDetails}>edit</button> */
}
<
button
onClick=
{
this
.
deleteBook
}
>
delete
</
button
>
<
button
onClick=
{
this
.
deleteBook
}
>
delete
</
button
>
</
div
>
</
div
>
</
main
>
</
main
>
...
...
app/javascript/components/library/BooksList.jsx
View file @
d41bb425
...
@@ -102,7 +102,7 @@ class BooksList extends Component{
...
@@ -102,7 +102,7 @@ class BooksList extends Component{
<
div
className=
"text-right mb-3"
>
<
div
className=
"text-right mb-3"
>
<
Link
to=
"/addBooks"
className=
"btn custom-button"
>
<
Link
to=
"/addBooks"
className=
"btn custom-button"
>
Add New book
Add New book
</
Link
>
</
Link
>
</
div
>
</
div
>
<
div
className=
"row"
>
<
div
className=
"row"
>
<
input
<
input
...
@@ -111,6 +111,7 @@ class BooksList extends Component{
...
@@ -111,6 +111,7 @@ class BooksList extends Component{
name=
"name"
name=
"name"
id=
"name"
id=
"name"
className=
"form-control"
className=
"form-control"
placeholder=
"search by bookname"
/>
/>
<
button
onClick=
{
this
.
searchByBookName
}
>
search
</
button
>
<
button
onClick=
{
this
.
searchByBookName
}
>
search
</
button
>
</
div
>
</
div
>
...
@@ -119,7 +120,9 @@ class BooksList extends Component{
...
@@ -119,7 +120,9 @@ class BooksList extends Component{
</
div
>
</
div
>
<
Link
to=
"/"
className=
"btn btn-link"
>
<
Link
to=
"/"
className=
"btn btn-link"
>
Home
Home
</
Link
>
</
Link
>
<
button
className=
"btn btn-link"
onClick=
{
this
.
props
.
history
.
goBack
}
>
Go Back
</
button
>
{
/* <Link to={this.props.history.goBack} className="btn btn-link"></Link> */
}
</
main
>
</
main
>
</
div
>
</
div
>
</>
</>
...
...
app/javascript/components/library/Library.jsx
View file @
d41bb425
...
@@ -13,13 +13,17 @@ class Library extends Component {
...
@@ -13,13 +13,17 @@ class Library extends Component {
<
main
className=
"container"
>
<
main
className=
"container"
>
<
div
className=
"text-right mb-3"
>
<
div
className=
"text-right mb-3"
>
Manage your books
Manage your books
</
div
>
</
div
>
<
Link
to=
"/booksList"
className=
"btn btn-link"
>
view all books
</
Link
>
<
Link
to=
"/addBooks"
className=
"btn btn-link"
>
<
Link
to=
"/addBooks"
className=
"btn btn-link"
>
create books
create books
</
Link
>
</
Link
>
<
Link
to=
"/"
className=
"btn btn-link"
>
<
Link
to=
"/"
className=
"btn btn-link"
>
Home
Home
</
Link
>
</
Link
>
</
main
>
</
main
>
</
div
>
</
div
>
</>)
</>)
...
...
spec/factories/book.rb
0 → 100644
View file @
d41bb425
FactoryBot
.
define
do
factory
:book
,
class:
Book
do
name
{
Faker
::
Name
.
name
}
writterName
{
Faker
::
Name
.
name
}
courseName
{
Faker
::
Name
.
name
}
end
end
\ No newline at end of file
spec/features/homepage._spec.rb
View file @
d41bb425
...
@@ -3,4 +3,5 @@ describe "GET '/' - from API", :type => :feature do
...
@@ -3,4 +3,5 @@ describe "GET '/' - from API", :type => :feature do
visit
(
'http://localhost:3000/'
)
visit
(
'http://localhost:3000/'
)
expect
(
page
).
to
have_title
"RailsReactRecipe"
expect
(
page
).
to
have_title
"RailsReactRecipe"
end
end
end
end
\ No newline at end of file
spec/features/library_spec.rb
0 → 100644
View file @
d41bb425
require
'rails_helper'
RSpec
.
feature
"Libraries"
,
type: :feature
do
context
"Adding books"
do
before
do
visit
'http://localhost:3000/addBooks'
end
scenario
"book should be added"
,
:js
do
expect
(
page
).
to
have_text
(
"ADD BOOk"
)
end
end
end
spec/models/book_spec.rb
View file @
d41bb425
require
'rails_helper'
require
'rails_helper'
RSpec
.
describe
Book
,
type: :model
do
RSpec
.
describe
Book
,
type: :model
do
pending
"add some examples to (or delete)
#{
__FILE__
}
"
describe
"Test Book Model Validation"
do
let
(
:book
)
{
build
(
:book
)
}
it
'should validate if bookname is nil'
do
book
.
name
=
nil
expect
(
book
.
save
).
to
eq
(
false
)
end
it
'should validate if writter name is nil'
do
book
.
writterName
=
nil
expect
(
book
.
save
).
to
be
(
false
)
end
it
'should validate if course name is nil'
do
book
.
courseName
=
nil
expect
(
book
.
save
).
to
eq
(
false
)
end
it
'should valid if book model fields are nil'
do
book
=
Book
.
new
(
name
:'Math'
,
writterName:
'jhjh'
,
courseName:
"nil"
)
expect
(
book
).
to
be_valid
end
it
'bookname & writter name shold not be same '
do
expect
(
book
.
name
).
to_not
be
(
book
.
writterName
)
end
it
"is not equal to another string of a different value"
do
expect
(
"this string"
).
not_to
eql
(
"a different string"
)
end
it
"is not equal to a float of the same value"
do
expect
(
5
).
not_to
eql
(
5.0
)
end
it
"is not equal to a float of the same value"
do
puts
book
.
name
puts
book
.
writterName
puts
book
.
courseName
# expect(book.name).to be==book.courseName
end
context
'Employee Table Operations Test Case'
do
it
'save book'
do
book
=
Book
.
new
(
name
:'Math'
,
writterName:
'jhjh'
,
courseName:
"nil"
).
save
expect
(
book
).
to
eq
(
true
)
end
it
'delete book'
do
book
=
Book
.
delete
(
name
:'Math'
)
expect
(
book
).
to
eq
(
0
)
end
end
end
end
end
spec/rails_helper.rb
View file @
d41bb425
...
@@ -5,6 +5,7 @@ require File.expand_path('../config/environment', __dir__)
...
@@ -5,6 +5,7 @@ require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
# Prevent database truncation if the environment is production
abort
(
"The Rails environment is running in production mode!"
)
if
Rails
.
env
.
production?
abort
(
"The Rails environment is running in production mode!"
)
if
Rails
.
env
.
production?
require
'rspec/rails'
require
'rspec/rails'
require
'capybara/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# Requires supporting ruby files with custom matchers and macros, etc, in
...
...
spec/requests/api/v1/book_request_spec.rb
View file @
d41bb425
...
@@ -4,14 +4,14 @@ RSpec.describe "Api::V1::Books", type: :request do
...
@@ -4,14 +4,14 @@ RSpec.describe "Api::V1::Books", type: :request do
describe
"GET /index"
do
describe
"GET /index"
do
it
"returns http success"
do
it
"returns http success"
do
get
"/api/v1/book/index"
get
"/api/v1/book
s
/index"
expect
(
response
).
to
have_http_status
(
:success
)
expect
(
response
).
to
have_http_status
(
:success
)
end
end
end
end
describe
"
GE
T /create"
do
describe
"
POS
T /create"
do
it
"returns http success"
do
it
"returns http success"
do
get
"/api/v1/book/create"
get
"/api/v1/book
s
/create"
expect
(
response
).
to
have_http_status
(
:success
)
expect
(
response
).
to
have_http_status
(
:success
)
end
end
end
end
...
@@ -23,11 +23,34 @@ RSpec.describe "Api::V1::Books", type: :request do
...
@@ -23,11 +23,34 @@ RSpec.describe "Api::V1::Books", type: :request do
end
end
end
end
describe
"
GET
/destroy"
do
describe
"
DELETE
/destroy"
do
it
"returns http success"
do
it
"returns http success"
do
get
"/api/v1/book/destroy"
get
"/api/v1/book/destroy"
expect
(
response
).
to
have_http_status
(
:success
)
expect
(
response
).
to
have_http_status
(
:success
)
end
end
end
end
describe
"GET /search"
do
it
"returns http success"
do
get
"/api/v1/book/search"
expect
(
response
).
to
have_http_status
(
:success
)
end
end
describe
"GET /update"
do
it
"returns http success"
do
get
"/api/v1/book/update"
expect
(
response
).
to
have_http_status
(
:success
)
end
end
# describe "Books" do
# describe 'routing' do
# it 'routes GET /book to books#show' do
# # get("/book").should route_to("books#index")
# expect(get: '/book').to route_to(controller: 'books', action: 'show')
# end
# end
# end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment