Skip to content

Commit

Permalink
[AAE-6269] Storybook stories for Like component (#7329)
Browse files Browse the repository at this point in the history
* [AAE-6269] added stories and mock, refactored tests

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

* [AAE-6269] hide nodeId property
  • Loading branch information
tomgny committed Oct 28, 2021
1 parent 400c2a4 commit 27812c6
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 17 deletions.
47 changes: 47 additions & 0 deletions 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<LikeComponent> = (args: LikeComponent) => ({
props: args
});

export const primary = template.bind({});
primary.args = {
nodeId: 'fake-like-node-id'
};
34 changes: 34 additions & 0 deletions 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}
}
};
60 changes: 60 additions & 0 deletions 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<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);
}
}
@@ -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>;
}
Expand Up @@ -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;

Expand Down Expand Up @@ -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
});
});

Expand All @@ -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
});
});
});
Expand Up @@ -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 {
Expand Down

0 comments on commit 27812c6

Please sign in to comment.