Skip to content

Commit

Permalink
nightscout#6701 Report storage tests (nightscout#6814)
Browse files Browse the repository at this point in the history
* nightscout#6701 Report storage tests

Functional and unit tests

* nightscout#6701 Test cleanup

* nightscout#6701 js-storage teardown

The first time js-storage is required it evaluates if it's running in the browser or not: https://github.com/julien-maurel/js-storage/blob/master/js.storage.js#L423 and will define the localstorage getters and setters accordingly. This becomes an issue if testing localstorage between UI and non-UI tests. reportstorage.test.js was requiring it before hashauth.test.js causing a conflict.

* nightscout#6701 false positive test

The page isn't refreshing

Co-authored-by: Sulka Haro <[email protected]>
  • Loading branch information
2 people authored and Alex Ovcinnikov committed Nov 6, 2021
1 parent 3d94b0b commit 9fdbd91
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/hashauth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('hashauth', function ( ) {
});

after(function (done) {
// cleanup js-storage as it evaluates if the test is running in the window or not when first required
delete require.cache[require.resolve('js-storage')];
done( );
});

Expand Down
60 changes: 60 additions & 0 deletions tests/reportstorage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const should = require('should');
const defaultValues = {
insulin: true,
carbs: true,
basal: true,
notes: false,
food: true,
raw: false,
iob: false,
cob: false,
predicted: false,
openAps: false,
insulindistribution: true,
predictedTruncate: true
};

describe('reportstorage unit tests', () => {
let reportstorage, storage, mockStorage;

beforeEach(() => {
reportstorage = require('../lib/report/reportstorage');
storage = require('js-storage').localStorage;
mockStorage = require('./fixtures/localstorage');
storage.get = mockStorage.get;
storage.set = mockStorage.set;
});

afterEach(() => {
delete require.cache[require.resolve('js-storage')];
delete require.cache[require.resolve('./fixtures/localstorage')];
delete require.cache[require.resolve('../lib/report/reportstorage')];
});

it('reportstorage definition - returns saveProps and getValue function', () => {
reportstorage.should.not.be.undefined();
(reportstorage.getValue instanceof Function).should.be.true();
(reportstorage.saveProps instanceof Function).should.be.true();
});

it('reportstorage.getValue returns default properties', () => {
let keyCount = 0;
for (const v in defaultValues) {
reportstorage.getValue(v).should.equal(defaultValues[v]);
keyCount++;
}
keyCount.should.equal(Object.keys(defaultValues).length);
});

it('reportstorage.saveProps sets property in localstorage', () => {
reportstorage.saveProps({insulin: false});
should.exist(mockStorage.get('reportProperties'));
mockStorage.get('reportProperties').insulin.should.be.false();
});

it('reportstorage.saveProps ignores property not tracked', () => {
reportstorage.saveProps({foo: 'bar'});
should.exist(mockStorage.get('reportProperties'));
should.not.exist(mockStorage.get('reportProperties').foo);
});
});

0 comments on commit 9fdbd91

Please sign in to comment.