mercredi 21 août 2019

How to test serializer when applying on the transformer in Emberjs

The problem is that I can't even run the test when I have a transform applying on the serializer. The errors show like this:

TypeError: typeClass.eachTransformedAttribute is not a function
    at Class.applyTransforms (http://localhost:7357/assets/vendor.js:116271:17)
    at Class.normalize (http://localhost:7357/assets/vendor.js:88027:12)
    at Context.<anonymous> (http://localhost:7357/assets/tests.js:854:41)
    at callFn (http://localhost:7357/assets/test-support.js:17929:21)
    at Test.Runnable.run (http://localhost:7357/assets/test-support.js:17922:7)
    at Runner.runTest (http://localhost:7357/assets/test-support.js:18392:10)
    at http://localhost:7357/assets/test-support.js:18498:12
    at next (http://localhost:7357/assets/test-support.js:18312:14)
    at http://localhost:7357/assets/test-support.js:18322:7
    at next (http://localhost:7357/assets/test-support.js:18254:14)


The version list:

    "ember-cli": "~3.11.0",
    "ember-mocha": "^0.16.0",
    "ember-cli-chai": "^0.5.0",
    "ember-cli-mirage": "^1.1.0",

unit/serializers/posts-test.js

import { expect } from "chai";
import { describe, it } from "mocha";
import { setupTest } from "ember-mocha";
import setupMirage from "ember-cli-mirage/test-support/setup-mirage";

describe("Unit | Serializer | Post", function() {
    let hooks = setupTest();
    setupMirage(hooks);

    it("serializers test", function(){
      let store = this.owner.lookup("service:store");

      let record = store.createRecord("post", {});

      let serializer = store.serializerFor("post");

      let input = {
         id: 888,
         title: "Good Day",
         content: "That's a good day."
      }

      let expectResult = {
         id: 888,
         data: {
           title: "Good Day",
           content: "That's a good day."
         }  
      }

      let normalize = serializer.normalize(record, input);

      expect(normalize).to.be.ok;

      expect(normalize).to.be.deepEqual(expectResult);
    });

app/serializers/post.js


import DS from "ember-data";

export default DS.RESTSerializer.extend({

   normalize(model, hash){
      const res = {
        id: hash.id,
        data: {
          title: hash.title,
          content: hash.content 
        }
      }

   this.applyTransforms(model, res.data); // I think the error start here

   return { data: res };   

   }

});


app/transforms/title.js

import DS from "ember-data";
import { A } from "@ember/array";
let Title = object.create({});

export default DS.Transform.extend({
    deserialize(serialized) {
        return Title.create(serialized);
    },

    serialize(deserialized) {
        return deserialized;
    }
});


app/transforms/content.js

import DS from "ember-data";
import { A } from "@ember/array";
let Content = object.create({});

export default DS.Transform.extend({
    deserialize(serialized) {
        return Content.create(serialized);
    },

    serialize(deserialized) {
        return deserialized;
    }
});



I can't even pass this serializer test, and I spent 5 days researching the easy approach to figure it out. unfortunately, I stuck on this question still.




Aucun commentaire:

Enregistrer un commentaire