Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
fix travis

docs: add cache unit test

test: add abc test
  • Loading branch information
cipchk committed Mar 13, 2018
1 parent b140bc5 commit 20b4fa0
Show file tree
Hide file tree
Showing 80 changed files with 3,547 additions and 873 deletions.
2 changes: 2 additions & 0 deletions .angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
"exclude": [
"./site/**/*",
"./src/app/**/*",
"./src/polyfills.ts",
"./src/test.ts",
"./src/core/cli/**/*"
]
}
Expand Down
30 changes: 21 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ language: node_js
node_js:
- "8.5.0"

env:
- TASK=build
- TASK=test
- TASK=lint

addons:
chrome: stable
apt:
sources:
- google-chrome
packages:
- google-chrome-stable
- google-chrome-beta

git:
depth: 1
Expand All @@ -14,13 +24,15 @@ before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

install:
- npm install
before_script:
- npm install -g @angular/cli
- npm install -g karma
- npm install -g npm-run-all
- npm install -g rimraf

script:
- npm run test
- npm run test-coverage

after_success:
- ./node_modules/.bin/codecov

- npm run $TASK
- |
if [ "$TASK" = "test" ]; then
cat ./coverage/lcov.info | ./node_modules/.bin/codecov
fi
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ delong is a production-ready solution for admin business components packages, Bu
[![Build Status](https://travis-ci.org/cipchk/delon.svg?branch=master)](https://travis-ci.org/cipchk/delon)
[![Dependency Status](https://david-dm.org/cipchk/delon/status.svg)](https://david-dm.org/cipchk/delon)
[![DevDependency Status](https://david-dm.org/cipchk/delon/dev-status.svg)](https://david-dm.org/cipchk/delon?type=dev)

[![codecov](https://codecov.io/gh/cipchk/delon/branch/master/graph/badge.svg)](https://codecov.io/gh/cipchk/delon)
[![npm](https://img.shields.io/npm/l/@delon/theme.svg)](https://www.npmjs.com/package/@delon/theme)
[![npm](https://img.shields.io/npm/dm/@delon/theme.svg)](https://www.npmjs.com/package/@delon/theme)

Expand Down
10 changes: 8 additions & 2 deletions docs/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ import { DelonCacheModule } from '@delon/mock';
| 参数名 | 类型 | 描述 |
| ----- | --- | --- |
| `key` | `string` | 缓存唯一标识符 |
| `data` | `any | Observable<any>` | 缓存数据源 |
| `data` | `any | Observable<any>` | 缓存数据源,数据源为 `Observable` 时,依然返回 `Observable`,否则返回 `void` |
| `options` | `{ type?: 'm' | 's', expire?: string }` | `type` 存储类型,'m' 表示内存,'s' 表示持久<br>`expire` 过期时间,例如:`10s` 10秒,单位值等同[moment](http://momentjs.cn/docs/#/manipulating/add/) |

**get()**

| 参数名 | 类型 | 描述 |
| ----- | --- | --- |
| `key` | `string` | 缓存唯一标识符 |
| `options` | `{ mode?: 'promise' | 'none', type?: 'm' | 's', expire?: string }` | `mode` 指定获取缓存的模式<br>`type` 存储类型,'m' 表示内存,'s' 表示持久<br>`expire` 过期时间,例如:`10s` 10秒,单位值等同[moment](http://momentjs.cn/docs/#/manipulating/add/) |
| `options` | `{ mode?: 'promise' | 'none', type?: 'm' | 's', expire?: string }` | `mode` 指定获取缓存的模式:<br>1、`promise` 表示若不存 `key` 则把 `key` 当URL发起请求并缓存且返回 Observable<br>2、`none` 表示直接返回数据若KEY不存在则直接返回 `null`<br><br>`type` 存储类型,'m' 表示内存,'s' 表示持久<br>`expire` 过期时间,例如:`10s` 10秒,单位值等同[moment](http://momentjs.cn/docs/#/manipulating/add/) |

**getNone()**

Expand All @@ -122,3 +122,9 @@ import { DelonCacheModule } from '@delon/mock';
**clear()**

清空所有缓存。

## 常见问题

### `get``tryGet` 的区别

本质都是获取并返回缓存数据,`get` 相比 `tryGet` 更简化,前者按KEY即是URL约定的风格,后者需指定数据源对象。
2 changes: 1 addition & 1 deletion docs/common.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const data = { name: 'cipchk', address: { city: 'shanghai', district: 'changning
变成可迭代对象:

```html
<div *ngFor="let item of data | keys">{{item.value.city}} {{item.value.district}}</div>
<div *ngFor="let item of data | keys">{{item.key}} {{item.value | json}}</div>
```

### 徽章 yn
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function(config) {
require('@angular/cli/plugins/karma')
],
coverageIstanbulReporter: {
reports: ['html', 'lcovonly'],
reports: [ 'html', 'lcovonly', 'text', 'text-summary' ],
fixWebpackSourcePaths: true
},
angularCli: {
Expand Down
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
"start": "ng serve",
"gulp": "gulp",
"prepush": "npm run lint",
"build": "npm run release:validity",
"lint": "run-s lint:ts lint:style",
"lint:ts": "ng lint",
"lint:ts": "tslint -p tsconfig.json -c tslint.json 'lib/**/*.ts' -e 'lib/**/*.spec.ts'",
"lint:style": "stylelint \"{src}/**/*.less\" --syntax less",
"lint-staged": "lint-staged",
"e2e": "ng e2e",
"test": "ng test -sr -sm=false",
"test-coverage": "ng test -sr -cc -sm=false",
"test": "ng test -sr -cc",
"site:clean": "rimraf site/app/routes/gen",
"site:gen": "gulp site:build",
"site:serve:cli": "ng serve --app site -o --bh /",
Expand Down Expand Up @@ -115,15 +115,15 @@
"@angular/language-service": "^5.0.0",
"@schematics/schematics": "0.0.11",
"@types/gulp": "^4.0.4",
"@types/jasmine": "~2.8.2",
"@types/jasminewd2": "~2.0.2",
"@types/jasmine": "~2.8.6",
"@types/jasminewd2": "~2.0.3",
"@types/jszip": "^3.1.2",
"@types/mockjs": "^1.0.0",
"@types/node": "~8.5.2",
"@types/webpack": "^3.0.14",
"chalk": "^2.3.0",
"codecov": "^3.0.0",
"codelyzer": "~4.0.2",
"codelyzer": "~4.2.1",
"del": "^3.0.0",
"gulp": "^3.9.1",
"gulp-bump": "^3.0.0",
Expand All @@ -140,12 +140,9 @@
"jsonml.js": "^0.1.0",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-coverage-istanbul-reporter": "^1.4.2",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-remap-istanbul": "^0.6.0",
"karma-sauce-launcher": "^1.2.0",
"mark-twain": "^2.0.2",
"mkdirp": "^0.5.1",
"mockjs": "^1.0.1-beta3",
Expand Down
12 changes: 7 additions & 5 deletions src/core/abc/avatar-list/avatar-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ describe('abc: avatar-list', () => {
fixture.detectChanges();
});

it('should be init', () => {
expect(context).not.toBeNull();
});

it('#size, should be [large] size', () => {
context.size = 'large';
fixture.detectChanges();
expect(dl.queryAll(By.css('.ant-avatar-lg')).length).toBe(5);
});

it('#size, should be [mini] size', () => {
context.size = 'mini';
fixture.detectChanges();
expect(dl.queryAll(By.css('.ant-avatar-sm')).length).toBe(5);
});
});

@Component({
Expand All @@ -40,5 +42,5 @@ describe('abc: avatar-list', () => {
</avatar-list>`
})
class TestComponent {
size: 'large' | 'small' | 'mini' | 'default' = 'mini';
size: 'large' | 'small' | 'mini' | 'default' = 'default';
}
88 changes: 88 additions & 0 deletions src/core/abc/count-down/count-down.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { Component, DebugElement, ViewChild } from '@angular/core';
import { AdCountDownModule } from './count-down.module';
import { By } from '@angular/platform-browser';
import * as moment from 'moment';

describe('abc: count-down', () => {
let fixture: ComponentFixture<TestComponent>;
let dl: DebugElement;
let context: TestComponent;

beforeEach(() => {
fixture = TestBed.configureTestingModule({
imports: [ AdCountDownModule.forRoot() ],
declarations: [ TestComponent ]
}).createComponent(TestComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
});

it('should be create an instance via [config]', (done: () => void) => {
spyOn(context, 'begin');
spyOn(context, 'notify');
spyOn(context, 'end');
context.config = {
leftTime: 2,
notify: [ 1.5 ]
};
fixture.detectChanges();
setTimeout(() => {
expect(context.begin).toHaveBeenCalled();
expect(context.notify).toHaveBeenCalled();
expect(context.end).toHaveBeenCalled();
done();
}, 2000 + 20);
});

it('should be create an instance via [target]', (done: () => void) => {
spyOn(context, 'end');
context.target = 1;
fixture.detectChanges();
setTimeout(() => {
expect(context.end).toHaveBeenCalled();
done();
}, 1000 + 20);
});

it('should be create an instance when target is date', (done: () => void) => {
spyOn(context, 'end');
context.target = moment().add(1, 's').toDate();
fixture.detectChanges();
setTimeout(() => {
expect(context.end).toHaveBeenCalled();
done();
}, 1000 + 20);
});
});

@Component({
template: `
<div *ngIf="config">
<count-down
[config]="config"
(begin)="begin()"
(end)="end()"
(notify)="notify($event)" style="font-size: 20px"></count-down>
</div>
<div *ngIf="target">
<count-down
[target]="target"
(begin)="begin()"
(end)="end()"
(notify)="notify($event)" style="font-size: 20px"></count-down>
</div>`
})
class TestComponent {
config: any;
target: number | Date;
notify(val: number) {
console.log(val);
}
begin() {
console.log('begin');
}
end() {
console.log('end');
}
}
5 changes: 2 additions & 3 deletions src/core/abc/desc-list/desc-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class DescListComponent implements OnChanges, OnInit {
this._classMap.forEach(cls => this.renderer.removeClass(this.el.nativeElement, cls));

this._classMap = [ 'desc-list', this.layout ];
if (this.size) this._classMap.push('desc-list-' + this.size);
if (this.size) this._classMap.push(this.size);

this._classMap.forEach(cls => this.renderer.addClass(this.el.nativeElement, cls));
}
Expand Down Expand Up @@ -88,8 +88,7 @@ export class DescListComponent implements OnChanges, OnInit {
}

ngOnChanges(changes: { [P in keyof this]?: SimpleChange } & SimpleChanges): void {
if (changes.size && !changes.size.firstChange)
this.setClass();
this.setClass();
if (changes.col)
this.setResponsive();
}
Expand Down
90 changes: 90 additions & 0 deletions src/core/abc/desc-list/desc-list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { Component, DebugElement } from '@angular/core';
import { AdDescListModule } from './desc-list.module';
import { By } from '@angular/platform-browser';

describe('abc: desc-list', () => {
let fixture: ComponentFixture<TestComponent>;
let dl: DebugElement;
let context: TestComponent;

beforeEach(() => {
fixture = TestBed.configureTestingModule({
imports: [ AdDescListModule.forRoot() ],
declarations: [ TestComponent ]
}).createComponent(TestComponent);
dl = fixture.debugElement;
context = fixture.componentInstance;
fixture.detectChanges();
});

it('should be create an instance', () => {
expect(context).not.toBeNull();
expect(dl.queryAll(By.css('#defaultDL .term')).length).toBe(3);
});

it('should be custome title template', () => {
expect(dl.queryAll(By.css('#custom-title-template #titleTpl')).length).toBe(1);
});

it('should be changed property', () => {
expect(dl.queryAll(By.css('.large')).length).toBe(1);
// because of two desc-list component
expect(dl.queryAll(By.css('.horizontal')).length).toBe(2);
expect(dl.queryAll(By.css('.vertical')).length).toBe(0);
context.size = 'small';
context.layout = 'vertical';
context.col = 5;
fixture.detectChanges();
expect(dl.queryAll(By.css('.large')).length).toBe(0);
expect(dl.queryAll(By.css('.horizontal')).length).toBe(1);
expect(dl.queryAll(By.css('.vertical')).length).toBe(1);
});
});

@Component({
template: `
<desc-list id="defaultDL"
[size]="size"
[title]="'title'"
[gutter]="gutter"
[layout]="layout"
[col]="col"
>
<desc-list-item term="Firefox">
A free, open source, cross-platform,
graphical web browser developed by the
Mozilla Corporation and hundreds of
volunteers.
</desc-list-item>
<desc-list-item term="Firefox">
A free, open source, cross-platform,
graphical web browser developed by the
Mozilla Corporation and hundreds of
volunteers.
</desc-list-item>
<desc-list-item [term]="termTpl">
<ng-template #termTpl><p id="termTpl">termTpl</p></ng-template>
</desc-list-item>
</desc-list>
<desc-list id="custom-title-template"
[title]="title"
[gutter]="gutter"
>
<ng-template #title>
<p id="titleTpl">titleTpl</p>
</ng-template>
<desc-list-item term="Firefox">
A free, open source, cross-platform,
graphical web browser developed by the
Mozilla Corporation and hundreds of
volunteers.
</desc-list-item>
</desc-list>`
})
class TestComponent {
size: 'small' | 'large' = 'large';
gutter = 32;
layout: 'horizontal' | 'vertical' = 'horizontal';
col = 3;
}
Loading

0 comments on commit 20b4fa0

Please sign in to comment.