Skip to content

Commit 7995159

Browse files
committed
test: added tests for createInstance and instance functions.
1 parent 4576554 commit 7995159

File tree

1 file changed

+80
-35
lines changed

1 file changed

+80
-35
lines changed

tests/angular-localForage.js

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -478,49 +478,94 @@ describe('Module: LocalForageModule', function() {
478478
});
479479
});
480480

481-
describe("instance", function () {
482-
var testInstance_1;
483-
var testInstance_2;
484-
it('should create new instance for a storeName', function () {
485-
testInstance_1 = $localForage.createInstance({
486-
storeName: 'TEST_INSTANCE_1'
481+
describe("createInstance", function () {
482+
beforeEach(function () {
483+
$localForage.createInstance({
484+
name: 'DUPLICATE_INSTANCE_NAME'
487485
});
488486
});
489-
it('should create new instance for another storeName in same name', function () {
490-
testInstance_2 = $localForage.createInstance({
491-
storeName: 'TEST_INSTANCE_2'
492-
});
487+
it('should create a new instance', function () {
488+
expect($localForage.createInstance({
489+
name: 'TEST_INSTANCE'
490+
})).toBeDefined();
493491
});
494-
it('should give error if user tries to create new instance with already created storeName', function () {
495-
var instance = $localForage.createInstance({
496-
storeName: 'TEST_INSTANCE_3'
497-
});
498-
expect(function () {
499-
var duplicateInstance = $localForage.createInstance({
500-
storeName: 'TEST_INSTANCE_3'
501-
});
502-
}).toThrowError();
492+
493+
it('should throw error if trying to create duplicate instance.',
494+
function () {
495+
expect($localForage.createInstance.bind($localForage, {
496+
name: 'DUPLICATE_INSTANCE_NAME'
497+
})).toThrowError(/already defined/);
498+
});
499+
500+
it('should create instance with same name, different storeName',
501+
function () {
502+
expect($localForage.createInstance.bind($localForage, {
503+
name: 'DUPLICATE_INSTANCE_NAME',
504+
storeName: 'DIFFERENT_STORE_NAME'
505+
})).not.toThrowError(/already defined/);
503506
});
504-
it('should get created instance', function () {
507+
});
508+
509+
describe("instance", function () {
510+
var $q, interval;
511+
beforeEach(function () {
512+
$localForage.createInstance({
513+
name: 'TEST_INSTANCE_1'
514+
});
505515
$localForage.createInstance({
506-
name: 'testName',
507-
storeName: 'TEST_INSTANCE_4'
516+
name: 'TEST_INSTANCE_2',
517+
storeName: 'TEST_STORE_NAME_1'
508518
});
509-
var instance = $localForage.instance({
510-
name: 'testName',
511-
storeName: 'TEST_INSTANCE_4'
519+
$localForage.createInstance({
520+
name: 'TEST_INSTANCE_2',
521+
storeName: 'TEST_STORE_NAME_2'
522+
});
523+
inject(function (_$q_) {
524+
$q = _$q_;
512525
});
526+
interval = triggerDigests();
527+
});
528+
529+
afterEach(function () {
530+
stopDigests(interval);
531+
});
532+
533+
it('should get instance by name', function () {
534+
expect($localForage.instance({
535+
name: 'TEST_INSTANCE_1'
536+
})).toBeDefined();
537+
});
538+
539+
it('should throw exception if instance not exists', function () {
540+
expect($localForage.instance.bind($localForage, {
541+
name: 'NON_EXISTENT'
542+
})).toThrowError();
513543
});
514-
it('should not get non-created instance', function () {
515-
expect(function () {
516-
$localForage.instance({storeName: 'TEST_INSTANCE_4'});
517-
}).toThrowError('No localForage instance of that name exists.');
518-
expect(function () {
519-
$localForage.instance({
520-
name: 'wrongTestName',
521-
storeName: 'TEST_INSTANCE_4'
522-
});
523-
}).toThrowError('No localForage instance of that name exists.');
544+
545+
it('should get instances with same name, different storeNames',
546+
function (done) {
547+
triggerDigests();
548+
var instance1 = $localForage.instance({
549+
name: 'TEST_INSTANCE_2',
550+
storeName: 'TEST_STORE_NAME_1'
551+
});
552+
var instance2 = $localForage.instance({
553+
name: 'TEST_INSTANCE_2',
554+
storeName: 'TEST_STORE_NAME_2'
555+
});
556+
$q.all([
557+
instance1.setItem('key', 'val1'),
558+
instance2.setItem('key', 'val2')
559+
]).then(function () {
560+
return $q.all([
561+
instance1.getItem('key').then(function (val) {
562+
expect(val).toEqual('val1');
563+
}),
564+
instance2.getItem('key').then(function (val) {
565+
expect(val).toEqual('val2');
566+
})
567+
]);
568+
}).then(done);
524569
});
525570
});
526571
});

0 commit comments

Comments
 (0)