[AAE-6269] Storybook stories for Like component (#7329)

* [AAE-6269] added stories and mock, refactored tests

* [AAE-6269] added interface for live and mock rating service

* [AAE-6269] hide nodeId property
This commit is contained in:
tomgny
2021-10-28 15:07:14 +02:00
committed by GitHub
parent 400c2a456a
commit 27812c618a
6 changed files with 172 additions and 17 deletions

View File

@@ -0,0 +1,47 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Meta, moduleMetadata, Story } from '@storybook/angular';
import { RatingService } from './services/rating.service';
import { LikeComponent } from './like.component';
import { RatingServiceMock } from './mock/rating.service.mock';
import { SocialModule } from './social.module';
export default {
component: LikeComponent,
title: 'Content Services/Components/Like',
decorators: [
moduleMetadata({
imports: [SocialModule],
providers: [
{ provide: RatingService, useClass: RatingServiceMock }
]
})
],
argTypes: {
nodeId: { table: { disable: true } }
}
} as Meta;
const template: Story<LikeComponent> = (args: LikeComponent) => ({
props: args
});
export const primary = template.bind({});
primary.args = {
nodeId: 'fake-like-node-id'
};

View File

@@ -0,0 +1,34 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export const ratingOneMock = {
entry: {
myRating: '1',
ratedAt: '2017-04-06T14:34:28.061+0000',
id: 'fiveStar',
aggregate: {numberOfRatings: 1, average: 1.0}
}
};
export const ratingThreeMock = {
entry: {
myRating: '3',
ratedAt: '2017-04-06T14:36:40.731+0000',
id: 'fiveStar',
aggregate: {numberOfRatings: 1, average: 3.0}
}
};

View File

@@ -0,0 +1,60 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { RatingEntry } from '@alfresco/js-api';
import { Observable, of } from 'rxjs';
import { ratingOneMock, ratingThreeMock } from './rating-response.mock';
import { RatingServiceInterface } from '../services/rating.service.interface';
@Injectable({
providedIn: 'root'
})
export class RatingServiceMock implements RatingServiceInterface {
getRating(nodeId: string, _ratingType: any): Observable<RatingEntry | {}> {
if (nodeId === 'fake-like-node-id') {
return of(ratingOneMock);
}
return of(ratingThreeMock);
}
postRating(nodeId: string, _ratingType: string, _vote: any): Observable<RatingEntry | {}> {
if (nodeId === 'ratingOneMock') {
ratingOneMock.entry.aggregate.numberOfRatings = 1;
ratingOneMock.entry.aggregate.average = 1.0;
return of(ratingOneMock);
}
ratingThreeMock.entry.aggregate.numberOfRatings = 1;
ratingThreeMock.entry.aggregate.average = 1.0;
return of(ratingThreeMock);
}
deleteRating(nodeId: string, _ratingType: any): Observable<any> {
if (nodeId === 'ratingOneMock') {
ratingOneMock.entry.aggregate.numberOfRatings = 0;
ratingOneMock.entry.aggregate.average = 0;
return of(ratingOneMock);
}
ratingThreeMock.entry.aggregate.numberOfRatings = 0;
ratingThreeMock.entry.aggregate.average = 0;
return of(ratingThreeMock);
}
}

View File

@@ -0,0 +1,26 @@
/*!
* @license
* Copyright 2019 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { RatingEntry } from '@alfresco/js-api';
import { Observable } from 'rxjs';
export interface RatingServiceInterface {
getRating(nodeId: string, ratingType: any): Observable<RatingEntry | {}>;
postRating(nodeId: string, ratingType: string, vote: any): Observable<RatingEntry | {}>;
deleteRating(nodeId: string, ratingType: any): Observable<any>;
}

View File

@@ -20,6 +20,7 @@ import { setupTestBed } from '@alfresco/adf-core';
import { RatingService } from './rating.service';
import { ContentTestingModule } from '../../testing/content.testing.module';
import { TranslateModule } from '@ngx-translate/core';
import { ratingOneMock, ratingThreeMock } from '../mock/rating-response.mock';
declare let jasmine: any;
@@ -58,14 +59,7 @@ describe('Rating service', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: {
'entry': {
myRating: '1',
'ratedAt': '2017-04-06T14:34:28.061+0000',
'id': 'fiveStar',
'aggregate': {'numberOfRatings': 1, 'average': 1.0}
}
}
responseText: ratingOneMock
});
});
@@ -81,14 +75,7 @@ describe('Rating service', () => {
jasmine.Ajax.requests.mostRecent().respondWith({
status: 200,
contentType: 'json',
responseText: {
'entry': {
'myRating': '3',
'ratedAt': '2017-04-06T14:36:40.731+0000',
'id': 'fiveStar',
'aggregate': {'numberOfRatings': 1, 'average': 3.0}
}
}
responseText: ratingThreeMock
});
});
});

View File

@@ -20,11 +20,12 @@ import { Injectable } from '@angular/core';
import { RatingEntry, RatingBody, RatingsApi } from '@alfresco/js-api';
import { from, throwError, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { RatingServiceInterface } from './rating.service.interface';
@Injectable({
providedIn: 'root'
})
export class RatingService {
export class RatingService implements RatingServiceInterface {
_ratingsApi: RatingsApi;
get ratingsApi(): RatingsApi {