es6-es7-object-integration-test.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright 2013-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @emails sema@fb.com javascript@lists.facebook.com
  10. */
  11. /*jshint evil:true*/
  12. require('mock-modules').autoMockOff();
  13. describe('es6-es7-object-integration-test', function() {
  14. var transformFn;
  15. var visitors;
  16. // These are placeholder variables in scope that we can use to assert that a
  17. // specific variable reference was passed, rather than an object clone of it.
  18. var x = 123456;
  19. var z = 345678;
  20. beforeEach(function() {
  21. require('mock-modules').dumpCache();
  22. transformFn = require('../../src/jstransform').transform;
  23. var conciseMethodVisitors = require('../es6-object-concise-method-visitors').visitorList;
  24. var shortObjectsVisitors = require('../es6-object-short-notation-visitors').visitorList;
  25. var spreadPropertyVisitors = require('../es7-spread-property-visitors').visitorList;
  26. visitors = spreadPropertyVisitors.concat(
  27. shortObjectsVisitors,
  28. conciseMethodVisitors
  29. );
  30. });
  31. function transform(code) {
  32. return transformFn(visitors, code).code;
  33. }
  34. it('handles spread with concise methods and short notation', function() {
  35. var code = 'var xyz = { ...x, y() { return 42; }, z }';
  36. var objectAssignMock = jest.genMockFunction();
  37. Object.assign = objectAssignMock;
  38. eval(transform(code));
  39. var assignCalls = objectAssignMock.mock.calls;
  40. expect(assignCalls.length).toBe(1);
  41. expect(assignCalls[0].length).toBe(3);
  42. expect(assignCalls[0][0]).toEqual({});
  43. expect(assignCalls[0][1]).toEqual(x);
  44. var trailingObject = assignCalls[0][2];
  45. expect(trailingObject.y()).toEqual(42);
  46. expect(trailingObject.z).toEqual(z);
  47. });
  48. it('does not call assign when there are no spread properties', function() {
  49. var code = 'var xyz = { x, y() { return 42 }, z }';
  50. var objectAssignMock = jest.genMockFunction();
  51. Object.assign = objectAssignMock;
  52. eval(transform(code));
  53. expect(objectAssignMock).not.toBeCalled();
  54. });
  55. });