Skip to content

Commit 8098a01

Browse files
authored
Merge pull request #55 from nebulab/add-snapshot-specs
Add snapshot specs for countries query
2 parents 8d882f2 + b8c71fd commit 8098a01

File tree

12 files changed

+194
-28
lines changed

12 files changed

+194
-28
lines changed

solidus_graphql_api.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ Gem::Specification.new do |s|
3333
s.add_development_dependency 'rubocop-rspec', '~> 1.33.0'
3434
s.add_development_dependency 'simplecov', '~> 0.16.1'
3535
s.add_development_dependency 'sqlite3', '~> 1.4.1'
36+
s.add_development_dependency 'timecop', '~> 0.9.1'
3637
s.add_development_dependency 'with_model', '~> 2.1.2'
3738
end

spec/integration/countries_spec.rb

+25-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,33 @@
22

33
require 'spec_helper'
44

5-
RSpec.describe "Countries" do
6-
include_examples 'query is successful', :countries do
7-
let(:country_nodes) { subject.data.countries.nodes }
8-
let(:state_nodes) { country_nodes.map { |c| c.states.nodes }.flatten }
9-
10-
before do
11-
create_list(:state, 2)
12-
create_list(:state, 2, country_iso: 'IT')
5+
RSpec.describe_query :countries do
6+
connection_field :countries, query: :countries, freeze_date: true do
7+
context 'when countries does not exists' do
8+
it { expect(subject.data.countries.nodes).to be_empty }
139
end
1410

15-
it { expect(country_nodes).to be_present }
11+
context 'when countries exists' do
12+
let!(:country) { create(:country, id: 1) }
1613

17-
it { expect(state_nodes).to be_present }
14+
before { create(:country, id: 2, iso: 'IT') }
15+
16+
it { is_expected.to match_response(:countries) }
17+
18+
connection_field :states, query: 'countries/states' do
19+
context 'when states does not exists' do
20+
it { is_expected.to match_response('countries/empty_states') }
21+
end
22+
23+
context 'when states exists' do
24+
before do
25+
create(:state, id: 1, country: country)
26+
create(:state, id: 2, country: country, state_code: 'CA')
27+
end
28+
29+
it { is_expected.to match_response('countries/states') }
30+
end
31+
end
32+
end
1833
end
1934
end

spec/lib/solidus_graphql_api/types/country_spec.rb

-7
This file was deleted.

spec/lib/solidus_graphql_api/types/query_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
RSpec.describe SolidusGraphqlApi::Types::Query do
66
subject { described_class.send(:new, {}, {}) }
77

8-
it { expect(described_class.method_defined?(:countries)).to be_truthy }
98
it { expect(described_class.method_defined?(:taxonomies)).to be_truthy }
109

1110
describe '#products' do

spec/spec_helper.rb

+21
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,40 @@
1515

1616
require "graphql/schema_comparator"
1717
require 'with_model'
18+
require 'timecop'
1819

1920
# Requires supporting ruby files with custom matchers and macros, etc,
2021
# in spec/support/ and its subdirectories.
2122
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
2223

2324
RSpec.configure do |config|
25+
DEFAULT_FREEZE_DATE = "21/12/2012 12:00:00"
26+
2427
config.infer_spec_type_from_file_location!
2528
config.use_transactional_fixtures = false
2629
config.include Helpers::Graphql
30+
config.include Matchers::Graphql
31+
32+
# They can be used in the specs to explain better the result of a Graphql query.
33+
config.alias_example_group_to :describe_query, type: :graphql_query
34+
config.alias_example_group_to :connection_field, type: :graphql_query
35+
config.alias_example_group_to :field, type: :graphql_query
2736

2837
config.before(:each) do
2938
BatchLoader::Executor.clear_current
3039
end
3140

41+
config.around(:each, type: :graphql_query) do |example|
42+
freeze_date = example.metadata[:freeze_date]
43+
44+
if freeze_date
45+
Timecop.freeze(DateTime.parse(freeze_date.is_a?(String) ? freeze_date : DEFAULT_FREEZE_DATE))
46+
example.run
47+
Timecop.return
48+
else
49+
example.run
50+
end
51+
end
52+
3253
config.extend WithModel
3354
end

spec/support/matchers/graphql.rb

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module Matchers
4+
module Graphql
5+
extend RSpec::Matchers::DSL
6+
7+
matcher :match_response do |query_result_file|
8+
match do |actual|
9+
template = File.read("spec/support/query_results/#{query_result_file}.json.erb")
10+
json_result = ERB.new(template).result_with_hash(@variables || {})
11+
12+
@expected = JSON.parse(json_result, object_class: @object_class || OpenStruct)
13+
14+
actual == @expected
15+
end
16+
17+
chain :with_args do |variables|
18+
@variables = variables
19+
end
20+
21+
chain :as do |object_class|
22+
@object_class = object_class
23+
end
24+
25+
failure_message_when_negated do |actual|
26+
"expected that #{actual} would not be equal of #{expected}"
27+
end
28+
29+
diffable
30+
attr_reader :expected
31+
end
32+
end
33+
end

spec/support/queries/countries.gql

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
query {
22
countries {
33
nodes {
4+
createdAt
45
id
5-
isoName
66
iso
77
iso3
8+
isoName
89
name
910
numcode
1011
statesRequired
11-
createdAt
1212
updatedAt
13-
states {
14-
nodes {
15-
name
16-
abbr
17-
createdAt
18-
updatedAt
19-
}
20-
}
2113
}
2214
}
2315
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
query {
2+
countries {
3+
nodes {
4+
id
5+
states {
6+
nodes {
7+
abbr
8+
createdAt
9+
id
10+
name
11+
updatedAt
12+
}
13+
}
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"data": {
3+
"countries": {
4+
"nodes": [
5+
{
6+
"createdAt": "2012-12-21T12:00:00Z",
7+
"id": 1,
8+
"iso": "US",
9+
"iso3": "USA",
10+
"isoName": "UNITED STATES",
11+
"name": "United States",
12+
"numcode": 840,
13+
"statesRequired": false,
14+
"updatedAt": "2012-12-21T12:00:00Z"
15+
},
16+
{
17+
"createdAt": "2012-12-21T12:00:00Z",
18+
"id": 2,
19+
"iso": "IT",
20+
"iso3": "ITA",
21+
"isoName": "ITALY",
22+
"name": "Italy",
23+
"numcode": 380,
24+
"statesRequired": false,
25+
"updatedAt": "2012-12-21T12:00:00Z"
26+
}
27+
]
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"data": {
3+
"countries": {
4+
"nodes": [
5+
{
6+
"id": 1,
7+
"states": {
8+
"nodes": []
9+
}
10+
},
11+
{
12+
"id": 2,
13+
"states": {
14+
"nodes": []
15+
}
16+
}
17+
]
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"data": {
3+
"countries": {
4+
"nodes": [
5+
{
6+
"id": 1,
7+
"states": {
8+
"nodes": [
9+
{
10+
"abbr": "AL",
11+
"createdAt": "2012-12-21T12:00:00Z",
12+
"id": 1,
13+
"name": "Alabama",
14+
"updatedAt": "2012-12-21T12:00:00Z"
15+
},
16+
{
17+
"abbr": "CA",
18+
"createdAt": "2012-12-21T12:00:00Z",
19+
"id": 2,
20+
"name": "California",
21+
"updatedAt": "2012-12-21T12:00:00Z"
22+
}
23+
]
24+
}
25+
},
26+
{
27+
"id": 2,
28+
"states": {
29+
"nodes": []
30+
}
31+
}
32+
]
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_context "graphql query subject", type: :graphql_query do
4+
subject do |example|
5+
execute_query(
6+
try(:query) || example.metadata[:query],
7+
variables: try(:query_variables) || example.metadata[:query_variables],
8+
context: try(:query_context) || example.metadata[:query_context]
9+
)
10+
end
11+
end

0 commit comments

Comments
 (0)