|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ProductTaxonomy |
| 6 | + module Serializers |
| 7 | + module Value |
| 8 | + module Data |
| 9 | + class DataSerializerTest < TestCase |
| 10 | + setup do |
| 11 | + @value1 = ProductTaxonomy::Value.new( |
| 12 | + id: 1, |
| 13 | + name: "Black", |
| 14 | + friendly_id: "color__black", |
| 15 | + handle: "color__black" |
| 16 | + ) |
| 17 | + @value2 = ProductTaxonomy::Value.new( |
| 18 | + id: 2, |
| 19 | + name: "Red", |
| 20 | + friendly_id: "color__red", |
| 21 | + handle: "color__red" |
| 22 | + ) |
| 23 | + @value3 = ProductTaxonomy::Value.new( |
| 24 | + id: 3, |
| 25 | + name: "Blue", |
| 26 | + friendly_id: "color__blue", |
| 27 | + handle: "color__blue" |
| 28 | + ) |
| 29 | + |
| 30 | + ProductTaxonomy::Value.add(@value1) |
| 31 | + ProductTaxonomy::Value.add(@value2) |
| 32 | + ProductTaxonomy::Value.add(@value3) |
| 33 | + end |
| 34 | + |
| 35 | + test "serialize returns the expected data structure for a value" do |
| 36 | + expected = { |
| 37 | + "id" => 1, |
| 38 | + "name" => "Black", |
| 39 | + "friendly_id" => "color__black", |
| 40 | + "handle" => "color__black" |
| 41 | + } |
| 42 | + |
| 43 | + assert_equal expected, DataSerializer.serialize(@value1) |
| 44 | + end |
| 45 | + |
| 46 | + test "serialize_all returns all values in data format" do |
| 47 | + expected = [ |
| 48 | + { |
| 49 | + "id" => 1, |
| 50 | + "name" => "Black", |
| 51 | + "friendly_id" => "color__black", |
| 52 | + "handle" => "color__black" |
| 53 | + }, |
| 54 | + { |
| 55 | + "id" => 2, |
| 56 | + "name" => "Red", |
| 57 | + "friendly_id" => "color__red", |
| 58 | + "handle" => "color__red" |
| 59 | + }, |
| 60 | + { |
| 61 | + "id" => 3, |
| 62 | + "name" => "Blue", |
| 63 | + "friendly_id" => "color__blue", |
| 64 | + "handle" => "color__blue" |
| 65 | + } |
| 66 | + ] |
| 67 | + |
| 68 | + assert_equal expected, DataSerializer.serialize_all |
| 69 | + end |
| 70 | + |
| 71 | + test "serialize_all sorts values by ID" do |
| 72 | + # Add values in random order to ensure sorting is by ID |
| 73 | + ProductTaxonomy::Value.reset |
| 74 | + ProductTaxonomy::Value.add(@value3) |
| 75 | + ProductTaxonomy::Value.add(@value1) |
| 76 | + ProductTaxonomy::Value.add(@value2) |
| 77 | + |
| 78 | + result = DataSerializer.serialize_all |
| 79 | + |
| 80 | + assert_equal 3, result.length |
| 81 | + assert_equal "color__black", result[0]["friendly_id"] |
| 82 | + assert_equal "color__red", result[1]["friendly_id"] |
| 83 | + assert_equal "color__blue", result[2]["friendly_id"] |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | +end |
0 commit comments