From 27812c618af63a114580dbdb28e71905fe84fa49 Mon Sep 17 00:00:00 2001 From: tomgny <49343696+tomgny@users.noreply.github.com> Date: Thu, 28 Oct 2021 15:07:14 +0200 Subject: [PATCH] [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 --- .../src/lib/social/like.component.stories.ts | 47 +++++++++++++++ .../lib/social/mock/rating-response.mock.ts | 34 +++++++++++ .../lib/social/mock/rating.service.mock.ts | 60 +++++++++++++++++++ .../services/rating.service.interface.ts | 26 ++++++++ .../social/services/rating.service.spec.ts | 19 +----- .../src/lib/social/services/rating.service.ts | 3 +- 6 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 lib/content-services/src/lib/social/like.component.stories.ts create mode 100644 lib/content-services/src/lib/social/mock/rating-response.mock.ts create mode 100644 lib/content-services/src/lib/social/mock/rating.service.mock.ts create mode 100644 lib/content-services/src/lib/social/services/rating.service.interface.ts diff --git a/lib/content-services/src/lib/social/like.component.stories.ts b/lib/content-services/src/lib/social/like.component.stories.ts new file mode 100644 index 0000000000..7d84af28ce --- /dev/null +++ b/lib/content-services/src/lib/social/like.component.stories.ts @@ -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 = (args: LikeComponent) => ({ + props: args +}); + +export const primary = template.bind({}); +primary.args = { + nodeId: 'fake-like-node-id' +}; diff --git a/lib/content-services/src/lib/social/mock/rating-response.mock.ts b/lib/content-services/src/lib/social/mock/rating-response.mock.ts new file mode 100644 index 0000000000..a247fbd667 --- /dev/null +++ b/lib/content-services/src/lib/social/mock/rating-response.mock.ts @@ -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} + } +}; diff --git a/lib/content-services/src/lib/social/mock/rating.service.mock.ts b/lib/content-services/src/lib/social/mock/rating.service.mock.ts new file mode 100644 index 0000000000..023c2cd9b1 --- /dev/null +++ b/lib/content-services/src/lib/social/mock/rating.service.mock.ts @@ -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 { + if (nodeId === 'fake-like-node-id') { + return of(ratingOneMock); + } + + return of(ratingThreeMock); + } + + postRating(nodeId: string, _ratingType: string, _vote: any): Observable { + 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 { + 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); + } +} diff --git a/lib/content-services/src/lib/social/services/rating.service.interface.ts b/lib/content-services/src/lib/social/services/rating.service.interface.ts new file mode 100644 index 0000000000..4e63b29389 --- /dev/null +++ b/lib/content-services/src/lib/social/services/rating.service.interface.ts @@ -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; + postRating(nodeId: string, ratingType: string, vote: any): Observable; + deleteRating(nodeId: string, ratingType: any): Observable; +} diff --git a/lib/content-services/src/lib/social/services/rating.service.spec.ts b/lib/content-services/src/lib/social/services/rating.service.spec.ts index 863c85a5d1..f3b576641b 100644 --- a/lib/content-services/src/lib/social/services/rating.service.spec.ts +++ b/lib/content-services/src/lib/social/services/rating.service.spec.ts @@ -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 }); }); }); diff --git a/lib/content-services/src/lib/social/services/rating.service.ts b/lib/content-services/src/lib/social/services/rating.service.ts index 1c2edf33b1..c1f0966615 100644 --- a/lib/content-services/src/lib/social/services/rating.service.ts +++ b/lib/content-services/src/lib/social/services/rating.service.ts @@ -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 {