File: event_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (76 lines) | stat: -rw-r--r-- 3,521 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Subscriptions::Event do
  class EventSchema < GraphQL::Schema

    class Query < GraphQL::Schema::Object
    end

    class JsonSubscription < GraphQL::Schema::Subscription
      argument :some_json, GraphQL::Types::JSON, required: false

      field :text, String, null: false
    end

    class Subscription < GraphQL::Schema::Object
      field :json_subscription, subscription: JsonSubscription
    end

    query(Query)
    subscription(Subscription)
  end

  it "should serialize a JSON argument into the topic name" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    event = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => { "b" => 1, "a" => 0 } }, field: field, context: nil, scope: nil)
    assert_equal %Q{:jsonSubscription:someJson:{"a":0,"b":1}}, event.topic
  end

  it "should not serialize the context into the topic name" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    context = { my_id: "abc" }
    event = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => { "b" => 1, "a" => 0 } }, field: field, context: context, scope: nil)
    assert_equal %Q{:jsonSubscription:someJson:{"a":0,"b":1}}, event.topic
    assert_equal event.context[:my_id], "abc"
  end

  it "should serialize two eqivalent JSON hashes with different key orderings into equivalent topic names" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    event_a = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => { "b" => 1, "a" => 0 } }, field: field, context: nil, scope: nil)
    event_b = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => { "a" => 0, "b" => 1 } }, field: field, context: nil, scope: nil)
    assert_equal event_a.topic, event_b.topic
  end

  it "should serialize nested hashes into their sorted key forms" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    nested_hash = {
      "b" => 1,
      "c" => {
        "z" => 100,
        "y" => 99
      },
      "a" => 0
    }
    event = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => nested_hash }, field: field, context: nil, scope: nil)
    assert_equal %Q{:jsonSubscription:someJson:{"a":0,"b":1,"c":{"y":99,"z":100}}}, event.topic
  end

  it "should serialize a hash inside an array as a sorted hash" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    event = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => [{ "b" => 1, "a" => 0 }] }, field: field, context: nil, scope: nil)
    assert_equal %Q{:jsonSubscription:someJson:[{"a":0,"b":1}]}, event.topic
  end

  it "should serialize a hash inside an array of an array as a sorted hash" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    event = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => [[{ "b" => 1, "a" => 0 }]] }, field: field, context: nil, scope: nil)
    assert_equal %Q{:jsonSubscription:someJson:[[{"a":0,"b":1}]]}, event.topic
  end

  it "should serialize a hash inside an array inside of a hash" do
    field = EventSchema.subscription.fields["jsonSubscription"]
    event = GraphQL::Subscriptions::Event.new(name: "test", arguments: { "someJson" => { "key" => [{ "b" => 1, "a" => 0}]} }, field: field, context: nil, scope: nil)
    assert_equal %Q{:jsonSubscription:someJson:{"key":[{"a":0,"b":1}]}}, event.topic
  end
end