cookie-test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. define(['test/test-helpers'], function(testHelpers) {
  3. var describeIf = testHelpers.describeIf;
  4. var it = testHelpers.itWithFreshLog;
  5. var originalConsole = window.console;
  6. var originalDocument = window.document;
  7. describeIf(testHelpers.isCookieStorageAvailable() && !testHelpers.isLocalStorageAvailable(),
  8. "Cookie-only persistence tests:", function() {
  9. beforeEach(function() {
  10. window.console = {"log" : jasmine.createSpy("console.log")};
  11. this.addMatchers({
  12. "toBeAtLevel" : testHelpers.toBeAtLevel,
  13. "toBeTheStoredLevel" : testHelpers.toBeTheLevelStoredByCookie
  14. });
  15. });
  16. afterEach(function() {
  17. window.console = originalConsole;
  18. });
  19. describe("If no level is saved", function() {
  20. beforeEach(function() {
  21. testHelpers.clearStoredLevels();
  22. });
  23. it("log level is set to warn by default", function(log) {
  24. expect(log).toBeAtLevel("warn");
  25. });
  26. it("warn is persisted as the current level", function(log) {
  27. expect("warn").toBeTheStoredLevel();
  28. });
  29. it("log can be set to info level", function(log) {
  30. log.setLevel("info");
  31. expect(log).toBeAtLevel("info");
  32. });
  33. it("log.setLevel() sets a cookie with the given level", function(log) {
  34. log.setLevel("debug");
  35. expect("debug").toBeTheStoredLevel();
  36. });
  37. });
  38. describe("If info level is saved", function() {
  39. beforeEach(function() {
  40. testHelpers.setStoredLevel("info");
  41. });
  42. it("info is the default log level", function(log) {
  43. expect(log).toBeAtLevel("info");
  44. });
  45. it("log can be changed to warn level", function(log) {
  46. log.setLevel("warn");
  47. expect(log).toBeAtLevel("warn");
  48. });
  49. it("log.setLevel() overwrites the saved level", function(log) {
  50. log.setLevel("error");
  51. expect("error").toBeTheStoredLevel();
  52. expect("info").not.toBeTheStoredLevel();
  53. });
  54. });
  55. describe("If the level is saved with other data", function() {
  56. beforeEach(function() {
  57. window.document.cookie = "qwe=asd";
  58. window.document.cookie = "loglevel=ERROR";
  59. window.document.cookie = "msg=hello world";
  60. });
  61. it("error is the default log level", function(log) {
  62. expect(log).toBeAtLevel("error");
  63. });
  64. it("log can be changed to silent level", function(log) {
  65. log.setLevel("silent");
  66. expect(log).toBeAtLevel("silent");
  67. });
  68. it("log.setLevel() overrides the saved level only", function(log) {
  69. log.setLevel("debug");
  70. expect('debug').toBeTheStoredLevel();
  71. expect(window.document.cookie).toContain("msg=hello world");
  72. });
  73. });
  74. describe("If the level cookie is set incorrectly", function() {
  75. beforeEach(function() {
  76. testHelpers.setCookieStoredLevel('gibberish');
  77. });
  78. it("warn is the default log level", function(log) {
  79. expect(log).toBeAtLevel("warn");
  80. });
  81. it("warn is persisted as the current level, overriding the invalid cookie", function(log) {
  82. expect("warn").toBeTheStoredLevel();
  83. });
  84. it("log can be changed to info level", function(log) {
  85. log.setLevel("info");
  86. expect(log).toBeAtLevel("info");
  87. });
  88. it("log.setLevel() overrides the saved level with the new level", function(log) {
  89. expect('debug').not.toBeTheStoredLevel();
  90. log.setLevel("debug");
  91. expect('debug').toBeTheStoredLevel();
  92. });
  93. });
  94. });
  95. });