Commit d41bb425 by Abhisarika Singh

worked on model using factories,api, controller & features testcases

parent 87e27025
...@@ -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
......
...@@ -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) {
...@@ -108,7 +108,7 @@ class AddBooks extends Component { ...@@ -108,7 +108,7 @@ 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>
......
...@@ -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>
......
...@@ -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>
...@@ -120,6 +121,8 @@ class BooksList extends Component{ ...@@ -120,6 +121,8 @@ class BooksList extends Component{
<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>
</> </>
......
...@@ -13,7 +13,11 @@ class Library extends Component { ...@@ -13,7 +13,11 @@ 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>
......
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
...@@ -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
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
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
...@@ -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
......
...@@ -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/books/index"
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
end end
end end
describe "GET /create" do describe "POST /create" do
it "returns http success" do it "returns http success" do
get "/api/v1/book/create" get "/api/v1/books/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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment