Skip to content

Commit

Permalink
Merge pull request #27 from ihadeed/master
Browse files Browse the repository at this point in the history
removed dependencies
made everything a dev dependency to make things simpler
added NgModule ( closes #25 )
added .editorconfig file
removed typings and added @types
compiling with ngc instead of tsc to produce needed files for AoT compilation
added tests
fix linting
add circleci instructions
  • Loading branch information
Nodonisko committed May 5, 2017
2 parents aef15b7 + 2201f16 commit 91edc0a
Show file tree
Hide file tree
Showing 16 changed files with 402 additions and 170 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2

# We recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
15 changes: 3 additions & 12 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
# Created by .ignore support plugin (hsz.mobi)
/.idea
.vscode/
# Node generated files
node_modules
npm-debug.log
# OS generated files
Thumbs.db
.DS_Store
# Ignored files
*.js
*.map
*.d.ts
typings
.idea
dist
aot
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

20 changes: 7 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,22 @@ And inject service to your app:
*app.module.ts*

```ts
import {CacheService} from "ionic-cache/ionic-cache";
import { CacheModule } from "ionic-cache";

@NgModule({
...
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage
imports: [
CacheModule.forRoot()
],
providers: [CacheService],
})
```

*app.component.ts*

```ts
import {CacheService} from "ionic-cache/ionic-cache";
import { CacheService } from "ionic-cache";

@App({
@Component({
templateUrl: "build/app.html"
})
class MyApp {
Expand All @@ -68,14 +65,11 @@ class MyApp {

```ts
...
import {CacheService} from "ionic-cache/ionic-cache";
import { CacheService } from "ionic-cache";

@Injectable()
export class SomeProvider {
constructor(http: Http, cache: CacheService) {
this.http = http;
this.cache = cache;
}
constructor(private http: Http, private cache: CacheService) {}

loadList() {
let url = "http://ip.jsontest.com";
Expand Down
24 changes: 24 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 2
jobs:
build:
working_directory: ~/ionic-cache/
docker:
- image: node:7
steps:
- checkout
- restore_cache:
key: node_modules_{{ checksum "package.json" }}
- run: npm install
- save_cache:
key: node_modules_{{ checksum "package.json" }}
paths:
- ~/ionic-cache/node_modules/
- run:
name: Lint
command: npm run lint
- run:
name: Test
command: npm test
- run:
name: Build
command: npm run build
2 changes: 0 additions & 2 deletions ionic-cache.ts

This file was deleted.

57 changes: 57 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"use strict";

module.exports = config => {

const conf = {
frameworks: [
'jasmine',
'karma-typescript'
],

plugins: [
'karma-typescript',
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-chrome-launcher'
],

preprocessors: {
'src/**/*.ts': ['karma-typescript']
},

karmaTypescriptConfig: {
bundlerOptions: {
entrypoints: /\.spec\.ts$/,
transforms: [
require("karma-typescript-angular2-transform")
]
},
compilerOptions: {
lib: ['es2015', 'dom']
}
},

files: [
{ pattern: 'src/**/*', included: true, watched: true },
],

reporters: ['progress'],

port: 9876,
colors: true,
logLevel: config.INFO,
autoWatch: true,
browsers: [
'Chrome',
// 'PhantomJS'
],
singleRun: false
};

if (process.env.CIRCLECI) {
conf.browsers = ['PhantomJS'];
}

config.set(conf);

};
48 changes: 33 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
"name": "ionic-cache",
"version": "1.1.2",
"description": "Ionic cache service - cache request, data, promises etc.",
"main": "ionic-cache.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "dist/index.js",
"files": [
"dist"
],
"scripts": {
"prepublish": "tsc"
"test": "karma start karma.conf.js --single-run",
"test:watch": "karma start karma.conf.js",
"build": "rm -rf dist aot && ngc",
"lint": "tslint \"src/**/*.ts\""
},
"repository": {
"type": "git",
Expand All @@ -22,19 +30,29 @@
"url": "https://github.com/Nodonisko/ionic-cache/issues"
},
"homepage": "https://github.com/Nodonisko/ionic-cache#readme",
"dependencies": {
"@angular/common": "^2.4.2",
"@angular/core": "^2.0.2",
"@angular/http": "^2.0.2",
"@angular/platform-browser": "^2.0.2",
"rxjs": "^5.0.0-beta.12",
"zone.js": "^0.7.4"
},
"devDependencies": {
"@angular/common": "^4.1.1",
"@angular/compiler": "^4.1.1",
"@angular/compiler-cli": "^4.1.1",
"@angular/core": "^4.1.1",
"@angular/http": "^4.1.1",
"@angular/platform-browser": "^4.1.1",
"@angular/platform-browser-dynamic": "^4.1.1",
"@types/jasmine": "^2.5.47",
"@types/node": "^7.0.18",
"@types/websql": "0.0.27",
"jasmine-core": "^2.6.1",
"karma": "^1.6.0",
"karma-chrome-launcher": "^2.1.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-typescript": "^3.0.1",
"karma-typescript-angular2-transform": "^1.0.0",
"rxjs": "^5.3.1",
"tslint": "^3.15.1",
"tslint-ionic-rules": "^0.0.6",
"typescript": "^2.0.3",
"typings": "^1.4.0"
},
"typings": "./ionic-cache.d.ts"
"tslint-ionic-rules": "^0.0.7",
"typescript": "^2.3.2",
"zone.js": "^0.8.10"
}
}
14 changes: 14 additions & 0 deletions src/cache.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CacheService } from './cache.service';

@NgModule()
export class CacheModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: CacheModule,
providers: [
CacheService
]
};
}
}
112 changes: 112 additions & 0 deletions src/cache.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import 'core-js';
import 'zone.js/dist/zone';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { CacheService, MESSAGES } from './cache.service';
import { async, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';

TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());

describe('CacheService', () => {

let service: CacheService;

beforeAll(() => {
service = new CacheService();
});

afterAll(async(() => {
service.clearAll();
}));

it('should create an instance of the service', () => expect(service).toBeDefined());

it('should save item to storage (async)', async(() => {
service.saveItem('name', 'ibby')
.then(() => expect(true).toBeTruthy())
.catch(() => expect(false).toBeTruthy());
}));

it('should get previously stored value (async)', async(() => {
service.getItem('name')
.then(value => expect(value).toEqual('ibby'))
.catch(() => expect(false).toBeTruthy());
}));

it('should disable cache', () => {
service.enableCache(false);
expect((<any> service).cacheEnabled === false).toBeTruthy();
});

it('should throw an error when getting item and cache is disabled', async(() => {
service.getItem('name')
.then(() => expect(false).toBeTruthy())
.catch((e) => expect(e).toEqual(MESSAGES[1]));
}));

it('should enable cache', () => {
service.enableCache(true);
expect((<any> service).cacheEnabled === true).toBeTruthy();
});

});

describe('Observable Caching', () => {

const key: string = 'http_cache_test';

let mockData: any = {
hello: 'world'
};

let observable = Observable.of(mockData);

let service: CacheService;

beforeAll(() => {
service = new CacheService();
});

beforeEach(() => {
spyOn(observable, 'subscribe').and.callThrough();
});

afterAll(function(done) {
service.clearAll()
.then(() => done())
.catch(() => done());
});

it('should create an instance of the service', () => {
expect(service).toBeDefined();
});

// it('', inject([XHRBackend, CacheService], (mockBackend: XHRBackend, cache: CacheService) => { }))

it('should return data from observable (async)', function(done) {
service.loadFromObservable(key, observable)
.subscribe(res => {
expect(res).toBeDefined();
expect(observable.subscribe).toHaveBeenCalled();
expect(res).toEqual(mockData);
done();
});
});

it('should return cached observable data (async)', function(done) {
service.loadFromObservable(key, observable)
.subscribe(res => {
expect(observable.subscribe).not.toHaveBeenCalled();
expect(res).toEqual(mockData);
done();
});
});

});
Loading

0 comments on commit 91edc0a

Please sign in to comment.