diff --git a/test/adapter/json_api/resource_identifier_test.rb b/test/adapter/json_api/resource_identifier_test.rb index 62b7d93b..1e93b26b 100644 --- a/test/adapter/json_api/resource_identifier_test.rb +++ b/test/adapter/json_api/resource_identifier_test.rb @@ -28,15 +28,25 @@ module ActiveModelSerializers end def test_defined_type - test_type(WithDefinedTypeSerializer, 'with-defined-type') + actual = actual_resource_identifier_object(WithDefinedTypeSerializer) + expected = { id: expected_model_id, type: 'with-defined-type' } + assert_equal actual, expected end def test_singular_type - test_type_inflection(AuthorSerializer, 'author', :singular) + actual = with_jsonapi_inflection :singular do + actual_resource_identifier_object(AuthorSerializer) + end + expected = { id: expected_model_id, type: 'author' } + assert_equal actual, expected end def test_plural_type - test_type_inflection(AuthorSerializer, 'authors', :plural) + actual = with_jsonapi_inflection :plural do + actual_resource_identifier_object(AuthorSerializer) + end + expected = { id: expected_model_id, type: 'authors' } + assert_equal actual, expected end def test_type_with_namespace @@ -60,49 +70,38 @@ module ActiveModelSerializers end def test_id_defined_on_object - test_id(AuthorSerializer, @model.id.to_s) + actual = actual_resource_identifier_object(AuthorSerializer) + expected = { id: @model.id.to_s, type: expected_model_type } + assert_equal actual, expected end def test_id_defined_on_serializer - test_id(WithDefinedIdSerializer, 'special_id') + actual = actual_resource_identifier_object(WithDefinedIdSerializer) + expected = { id: 'special_id', type: expected_model_type } + assert_equal actual, expected end def test_id_defined_on_fragmented - test_id(FragmentedSerializer, 'special_id') + actual = actual_resource_identifier_object(FragmentedSerializer) + expected = { id: 'special_id', type: expected_model_type } + assert_equal actual, expected end private - def test_type_inflection(serializer_class, expected_type, inflection) - original_inflection = ActiveModelSerializers.config.jsonapi_resource_type - ActiveModelSerializers.config.jsonapi_resource_type = inflection - test_type(serializer_class, expected_type) - ensure - ActiveModelSerializers.config.jsonapi_resource_type = original_inflection - end - - def test_type(serializer_class, expected_type) + def actual_resource_identifier_object(serializer_class) serializer = serializer_class.new(@model) resource_identifier = ResourceIdentifier.new(serializer, nil) - expected = { - id: @model.id.to_s, - type: expected_type - } - - assert_equal(expected, resource_identifier.as_json) + resource_identifier.as_json end - def test_id(serializer_class, id) - serializer = serializer_class.new(@model) - resource_identifier = ResourceIdentifier.new(serializer, nil) + def expected_model_type inflection = ActiveModelSerializers.config.jsonapi_resource_type - type = @model.class.model_name.send(inflection) - expected = { - id: id, - type: type - } + @model.class.model_name.send(inflection) + end - assert_equal(expected, resource_identifier.as_json) + def expected_model_id + @model.id.to_s end end end diff --git a/test/adapter/json_api/type_test.rb b/test/adapter/json_api/type_test.rb index 40b84cf2..8d857dd8 100644 --- a/test/adapter/json_api/type_test.rb +++ b/test/adapter/json_api/type_test.rb @@ -20,13 +20,13 @@ module ActiveModel end def test_config_plural - with_jsonapi_resource_type :plural do + with_jsonapi_inflection :plural do assert_type(@author, 'authors') end end def test_config_singular - with_jsonapi_resource_type :singular do + with_jsonapi_inflection :singular do assert_type(@author, 'author') end end @@ -46,14 +46,6 @@ module ActiveModel hash = serializable(resource, opts).serializable_hash assert_equal(expected_type, hash.fetch(:data).fetch(:type)) end - - def with_jsonapi_resource_type(inflection) - old_inflection = ActiveModelSerializers.config.jsonapi_resource_type - ActiveModelSerializers.config.jsonapi_resource_type = inflection - yield - ensure - ActiveModelSerializers.config.jsonapi_resource_type = old_inflection - end end end end diff --git a/test/support/serialization_testing.rb b/test/support/serialization_testing.rb index 524a3297..4a957976 100644 --- a/test/support/serialization_testing.rb +++ b/test/support/serialization_testing.rb @@ -47,6 +47,14 @@ module SerializationTesting ActiveModelSerializers.config.replace(old_config) end + def with_jsonapi_inflection(inflection) + original_inflection = ActiveModelSerializers.config.jsonapi_resource_type + ActiveModelSerializers.config.jsonapi_resource_type = inflection + yield + ensure + ActiveModelSerializers.config.jsonapi_resource_type = original_inflection + end + def with_serializer_lookup_disabled original_serializer_lookup = ActiveModelSerializers.config.serializer_lookup_enabled ActiveModelSerializers.config.serializer_lookup_enabled = false