-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AlexeyMatskevich
committed
Nov 26, 2019
1 parent
8820916
commit f9a46eb
Showing
6 changed files
with
123 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Mutations | ||
class CreateReview < BaseMutation | ||
argument :body, String, required: true do | ||
description "New products name" | ||
end | ||
|
||
argument :rating, Integer, required: true do | ||
description "New products weight" | ||
end | ||
|
||
argument :product_id, ID, required: true do | ||
description "New products description for preview page" | ||
end | ||
|
||
field :review, Types::ReviewType, null: true | ||
|
||
def resolve(body:, rating:, product_id:) | ||
review = Review.new(body: body, rating: rating, product_id: product_id, user: context[:current_user]) | ||
|
||
authorize! review, to: :create? | ||
|
||
if review.save | ||
valid(review) | ||
else | ||
invalid(review) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ReviewPolicy < ApplicationPolicy | ||
def create? | ||
true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Mutations::CreateReview do | ||
subject { described_class } | ||
|
||
it { is_expected.to have_a_field(:review).of_type(Types::ReviewType) } | ||
|
||
describe "create Review" do | ||
let(:user) { create(:user) } | ||
let(:product) { create(:product) } | ||
let(:mutation_type) { "createReview" } | ||
let(:mutation_string) { | ||
<<-GRAPHQL | ||
mutation createReview($body: String!, $rating: Int!, $productId: ID!) { | ||
createReview(body: $body, rating: $rating, productId: $productId) { | ||
review { | ||
body | ||
rating | ||
product { | ||
id | ||
} | ||
} | ||
errors { | ||
message | ||
field | ||
details | ||
} | ||
success | ||
} | ||
} | ||
GRAPHQL | ||
} | ||
|
||
before do | ||
mutation mutation_string, | ||
variables: { | ||
body: "Example review", | ||
rating: 4, | ||
productId: product.id, | ||
}, | ||
context: {current_user: user} | ||
end | ||
|
||
let(:expected_result) { | ||
{ | ||
"review" => { | ||
"body" => "Example review", | ||
"rating" => 4, | ||
"product" => { | ||
"id" => product.id.to_s | ||
} | ||
}, | ||
"errors" => [], | ||
"success" => true | ||
} | ||
} | ||
it "return the product object" do | ||
expect(gql_response.data[mutation_type]).to eq expected_result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "rails_helper" | ||
require "action_policy/rspec/dsl" | ||
|
||
describe ReviewPolicy do | ||
let(:user) { build_stubbed :user } | ||
let(:record) { build_stubbed :review } | ||
|
||
let(:context) { {user: user} } | ||
|
||
describe_rule :create? do | ||
succeed "when user with user role" | ||
end | ||
end |