Skip to content

Commit

Permalink
fix: global access to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
matiaslopezd authored Oct 11, 2021
1 parent 7d3dce1 commit 9b03230
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
// Polyfill window
global.window = {};
global.document = {};
// Polyfills

window.localStorage = {
global.document = {
createElement: () => ({
addEventListener: () => {},
style: {},
classList: { add: () => {}, remove: () => {} },
setAttribute: () => {},
remove: () => {},
}),
body: {
appendChild: () => {},
},
};

global.localStorage = {
store: {},
getItem: (key = '') => window.localStorage.store[key],
getItem: (key = '') => localStorage.store[key],
setItem: (key = '', value = '') => {
window.localStorage.store[key] = value;
localStorage.store[key] = value;
},
removeItem: (key = '') => {
delete window.localStorage.store[key];
delete localStorage.store[key];
}
};

window.sessionStorage = {
global.sessionStorage = {
store: {},
getItem: (key = '') => window.sessionStorage.store[key],
getItem: (key = '') => sessionStorage.store[key],
setItem: (key = '', value = '') => {
window.sessionStorage.store[key] = value;
sessionStorage.store[key] = value;
},
removeItem: (key = '') => {
delete window.sessionStorage.store[key];
delete sessionStorage.store[key];
}
};

window.navigator = {
global.navigator = {
language: 'en',
};

window.document = {
global.document = {
createElement: () => ({
addEventListener: () => {},
style: {},
Expand All @@ -41,7 +52,7 @@ window.document = {
},
};

window.location = {
global.location = {
ancestorOrigins: {},
assign: () => {},
reload: () => {},
Expand All @@ -58,10 +69,18 @@ window.location = {
search: '',
};

window.top = {
location: window.location,
global.top = {
location,
};

window.parent = {
location: window.location,
global.parent = {
location,
}

global.window = {
localStorage,
sessionStorage,
navigator,
document,
localtion,
};

0 comments on commit 9b03230

Please sign in to comment.