not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB

  1. define(["sugar-web/env"], function (env) {
  2. 'use strict';
  3. describe("getObjectId", function () {
  4. it("should return objectId from the sugar's environment", function () {
  5. var environment = {
  6. objectId: "objectId"
  7. };
  8. spyOn(env, "getEnvironment").andCallFake(function (callback) {
  9. setTimeout(function () {
  10. callback(null, environment);
  11. }, 0);
  12. });
  13. var expected_objectId;
  14. runs(function () {
  15. env.getObjectId(function (objectId) {
  16. expected_objectId = objectId;
  17. });
  18. });
  19. waitsFor(function () {
  20. return expected_objectId !== undefined;
  21. }, "should return objectId");
  22. });
  23. });
  24. describe("standalone mode", function () {
  25. it("should return true if in standalone mode", function () {
  26. var noWebActivityURLScheme = "http:";
  27. spyOn(env, 'getURLScheme').andReturn(noWebActivityURLScheme);
  28. var isStandaloneMode = env.isStandalone();
  29. expect(isStandaloneMode).toBe(true);
  30. });
  31. it("should return false if not in standalone mode", function () {
  32. var webActivityURLScheme = "activity:";
  33. spyOn(env, 'getURLScheme').andReturn(webActivityURLScheme);
  34. var isStandaloneMode = env.isStandalone();
  35. expect(isStandaloneMode).toBe(false);
  36. });
  37. });
  38. describe("getEnvironment", function () {
  39. var sugarOrig;
  40. beforeEach(function () {
  41. sugarOrig = JSON.parse(JSON.stringify(window.top.sugar));
  42. });
  43. afterEach(function () {
  44. window.top.sugar = sugarOrig;
  45. });
  46. describe("in sugar mode", function () {
  47. beforeEach(function () {
  48. spyOn(env, 'isStandalone').andReturn(false);
  49. });
  50. describe("when env was already set", function () {
  51. it("should run callback with null error and env", function () {
  52. var environment = {};
  53. window.top.sugar = {
  54. environment: environment
  55. };
  56. var callback = jasmine.createSpy();
  57. runs(function () {
  58. env.getEnvironment(callback);
  59. });
  60. waitsFor(function () {
  61. return callback.wasCalled;
  62. }, "callback should be executed");
  63. runs(function () {
  64. expect(callback).toHaveBeenCalledWith(
  65. null, environment);
  66. });
  67. });
  68. });
  69. describe("when env was not set, yet", function () {
  70. beforeEach(function () {
  71. window.top.sugar = undefined;
  72. });
  73. it("should set onEnvironmentSet handler", function () {
  74. var sugar;
  75. env.getEnvironment(function () {});
  76. sugar = window.top.sugar;
  77. expect(sugar.onEnvironmentSet).not.toBeUndefined();
  78. });
  79. it("should run callback on EnvironmentSet event", function () {
  80. var callback = jasmine.createSpy();
  81. var expectedEnv = "env";
  82. env.getEnvironment(callback);
  83. window.top.sugar.environment = expectedEnv;
  84. window.top.sugar.onEnvironmentSet();
  85. expect(callback).toHaveBeenCalledWith(null, expectedEnv);
  86. });
  87. });
  88. });
  89. it("should return {} in standalone mode", function () {
  90. window.top.sugar = undefined;
  91. spyOn(env, 'isStandalone').andReturn(true);
  92. var actualEnv;
  93. runs(function () {
  94. env.getEnvironment(function (error, environment) {
  95. actualEnv = environment;
  96. });
  97. });
  98. waitsFor(function () {
  99. return actualEnv !== undefined;
  100. }, "environment not to be undefined");
  101. runs(function () {
  102. expect(actualEnv).toEqual({});
  103. });
  104. });
  105. });
  106. });