• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

javierbrea / cypress-localstorage-commands / 3880130374

pending completion
3880130374

push

github

GitHub
Merge pull request #550 from javierbrea/renovate/eslint-plugin-jest-27.x

29 of 29 branches covered (100.0%)

Branch coverage included in aggregate %.

77 of 77 relevant lines covered (100.0%)

15.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/LocalStorage.js
1
const {
2
  GET_SNAPSHOT_TASK,
3
  SET_SNAPSHOT_TASK,
4
  CLEAR_SNAPSHOT_TASK,
5
  NODE_EVENTS_INSTALLED,
6
} = require("./constants");
3✔
7

8
const LOCAL_STORAGE_METHODS = ["setItem", "getItem", "removeItem", "clear"];
3✔
9

10
function logDisabled(method) {
11
  return function () {
72✔
12
    this._cy.log(`localStorage.${method} is disabled`);
4✔
13
  };
14
}
15

16
function logDisabledMethodName(localStorageMethod) {
17
  return `_log${localStorageMethod}Disabled`;
108✔
18
}
19

20
class LocalStorage {
21
  static get cypressCommands() {
22
    return [
5✔
23
      "clearLocalStorageSnapshot",
24
      "saveLocalStorage",
25
      "restoreLocalStorage",
26
      "setLocalStorage",
27
      "getLocalStorage",
28
      "removeLocalStorage",
29
      "disableLocalStorage",
30
    ];
31
  }
32

33
  constructor(localStorage, cy, Cypress) {
34
    this._nodeEventsInstalled = Cypress.env(NODE_EVENTS_INSTALLED) === true;
18✔
35
    this._snapshot = {};
18✔
36
    this._namedSnapshots = {};
18✔
37
    this._cy = cy;
18✔
38
    this._localStorage = localStorage;
18✔
39

40
    LOCAL_STORAGE_METHODS.forEach((localStorageMethod) => {
18✔
41
      this[logDisabledMethodName(localStorageMethod)] = logDisabled(localStorageMethod).bind(this);
72✔
42
    });
43
  }
44

45
  _saveLocalStorageKeyToMemory(key, snapshotName) {
46
    if (snapshotName) {
63✔
47
      this._namedSnapshots[snapshotName][key] = this._localStorage.getItem(key);
24✔
48
    } else {
49
      this._snapshot[key] = this._localStorage.getItem(key);
39✔
50
    }
51
  }
52

53
  _saveLocalStorageToMemory(snapshotName) {
54
    Object.keys(this._localStorage).forEach((key) => {
11✔
55
      this._saveLocalStorageKeyToMemory(key, snapshotName);
63✔
56
    });
57
  }
58

59
  _clearMemorySnapshot(snapshotName) {
60
    if (snapshotName) {
15✔
61
      this._namedSnapshots[snapshotName] = {};
6✔
62
    } else {
63
      this._snapshot = {};
9✔
64
    }
65
  }
66

67
  _getSnapshotFromMemory(snapshotName) {
68
    return snapshotName ? this._namedSnapshots[snapshotName] : this._snapshot;
20✔
69
  }
70

71
  _restoreLocalStorageFromSnapshot(obj = {}) {
1✔
72
    Object.keys(obj).forEach((key) => {
27✔
73
      this._localStorage.setItem(key, obj[key]);
88✔
74
    });
75
  }
76

77
  _restoreLocalStorageFromMemory(snapshotName) {
78
    this._restoreLocalStorageFromSnapshot(this._getSnapshotFromMemory(snapshotName));
16✔
79
  }
80

81
  _copySnapshotFromMemoryToNode(snapshotName) {
82
    if (this._nodeEventsInstalled) {
11✔
83
      return this._cy.task(SET_SNAPSHOT_TASK, {
4✔
84
        name: snapshotName,
85
        snapshot: this._getSnapshotFromMemory(snapshotName),
86
      });
87
    }
88
  }
89

90
  _clearNodeSnapshot(snapshotName) {
91
    if (this._nodeEventsInstalled) {
15✔
92
      return this._cy.task(CLEAR_SNAPSHOT_TASK, snapshotName);
6✔
93
    }
94
  }
95

96
  _restoreLocalStorageFromNode(snapshotName) {
97
    return this._cy.task(GET_SNAPSHOT_TASK, snapshotName).then((snapshot) => {
11✔
98
      this._restoreLocalStorageFromSnapshot(snapshot);
11✔
99
    });
100
  }
101

102
  clearLocalStorageSnapshot(snapshotName) {
103
    this._clearMemorySnapshot(snapshotName);
15✔
104
    return this._clearNodeSnapshot(snapshotName);
15✔
105
  }
106

107
  saveLocalStorage(snapshotName) {
108
    if (!this._localStorage.getItem.wrappedMethod) {
12✔
109
      this.clearLocalStorageSnapshot(snapshotName);
11✔
110
      this._saveLocalStorageToMemory(snapshotName);
11✔
111
      return this._copySnapshotFromMemoryToNode(snapshotName);
11✔
112
    }
113
  }
114

115
  restoreLocalStorage(snapshotName) {
116
    this._localStorage.clear();
27✔
117
    if (this._nodeEventsInstalled) {
27✔
118
      return this._restoreLocalStorageFromNode(snapshotName);
11✔
119
    } else {
120
      this._restoreLocalStorageFromMemory(snapshotName);
16✔
121
    }
122
  }
123

124
  getLocalStorage(key) {
125
    return this._localStorage.getItem(key);
7✔
126
  }
127

128
  setLocalStorage(key, value) {
129
    return this._localStorage.setItem(key, value);
8✔
130
  }
131

132
  removeLocalStorage(key) {
133
    return this._localStorage.removeItem(key);
2✔
134
  }
135

136
  disableLocalStorage(options = {}) {
13✔
137
    this._cy.on("window:before:load", (win) => {
14✔
138
      if (
13✔
139
        win.localStorage &&
34✔
140
        !win.localStorage[LOCAL_STORAGE_METHODS[0]].wrappedMethod &&
141
        !this._localStorage[LOCAL_STORAGE_METHODS[0]].wrappedMethod
142
      ) {
143
        LOCAL_STORAGE_METHODS.forEach((localStorageMethod) => {
9✔
144
          this._cy
36✔
145
            .stub(this._localStorage, localStorageMethod)
146
            .callsFake(this[logDisabledMethodName(localStorageMethod)]);
147
          this._cy.stub(win.localStorage, localStorageMethod).throws(options.withError);
36✔
148
        });
149
      }
150
    });
151
  }
152
}
153

154
module.exports = LocalStorage;
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc