github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/unit/serializers/application-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupTest } from 'ember-qunit';
     3  import ApplicationSerializer from 'nomad-ui/serializers/application';
     4  
     5  import Model from '@ember-data/model';
     6  import { attr } from '@ember-data/model';
     7  import classic from 'ember-classic-decorator';
     8  
     9  @classic
    10  class TestSerializer extends ApplicationSerializer {
    11    arrayNullOverrides = ['Things'];
    12  
    13    mapToArray = [
    14      'ArrayableMap',
    15      {
    16        beforeName: 'OriginalNameArrayableMap',
    17        afterName: 'RenamedArrayableMap',
    18      },
    19    ];
    20  
    21    separateNanos = ['Time'];
    22  }
    23  
    24  class TestModel extends Model {
    25    @attr() things;
    26  
    27    @attr() arrayableMap;
    28    @attr() renamedArrayableMap;
    29  
    30    @attr() time;
    31    @attr() timeNanos;
    32  }
    33  
    34  module('Unit | Serializer | Application', function (hooks) {
    35    setupTest(hooks);
    36  
    37    hooks.beforeEach(function () {
    38      this.store = this.owner.lookup('service:store');
    39      this.owner.register('model:test', TestModel);
    40      this.owner.register('serializer:test', TestSerializer);
    41  
    42      this.subject = () => this.store.serializerFor('test');
    43    });
    44  
    45    const normalizationTestCases = [
    46      {
    47        name: 'Null array and maps',
    48        in: {
    49          ID: 'test-test',
    50          Things: null,
    51          ArrayableMap: null,
    52          OriginalNameArrayableMap: null,
    53          Time: 1607839992000100000,
    54        },
    55        out: {
    56          data: {
    57            id: 'test-test',
    58            attributes: {
    59              things: [],
    60              arrayableMap: [],
    61              renamedArrayableMap: [],
    62              time: 1607839992000,
    63              timeNanos: 100096,
    64            },
    65            relationships: {},
    66            type: 'test',
    67          },
    68        },
    69      },
    70      {
    71        name: 'Non-null array and maps',
    72        in: {
    73          ID: 'test-test',
    74          Things: [1, 2, 3],
    75          ArrayableMap: {
    76            b: { Order: 2 },
    77            a: { Order: 1 },
    78            'c.d': { Order: 3 },
    79          },
    80          OriginalNameArrayableMap: {
    81            a: { X: 1 },
    82          },
    83          Time: 1607839992000100000,
    84          SomethingExtra: 'xyz',
    85        },
    86        out: {
    87          data: {
    88            id: 'test-test',
    89            attributes: {
    90              things: [1, 2, 3],
    91              arrayableMap: [
    92                { Name: 'a', Order: 1 },
    93                { Name: 'b', Order: 2 },
    94                { Name: 'c.d', Order: 3 },
    95              ],
    96              renamedArrayableMap: [{ Name: 'a', X: 1 }],
    97              time: 1607839992000,
    98              timeNanos: 100096,
    99            },
   100            relationships: {},
   101            type: 'test',
   102          },
   103        },
   104      },
   105    ];
   106  
   107    normalizationTestCases.forEach((testCase) => {
   108      test(`normalization: ${testCase.name}`, async function (assert) {
   109        assert.deepEqual(
   110          this.subject().normalize(TestModel, testCase.in),
   111          testCase.out
   112        );
   113      });
   114    });
   115  });