...
 
Commits (2)
import { TestBed } from '@angular/core/testing';
import { TagsPipe } from './tags';
import { FeaturesService } from '../../services/features.service';
import { MockService } from '../../utils/mock';
import { SiteService } from '../services/site.service';
describe('TagPipe', () => {
let featuresServiceMock: any = MockService(FeaturesService, {
const featuresServiceMock: any = MockService(FeaturesService, {
has: feature => {
return true;
},
});
const siteServiceMock: any = MockService(SiteService, {
props: {
isProDomain: { get: () => false },
pro: { get: () => false },
},
});
let pipe;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [TagsPipe],
providers: [
{
provide: FeaturesService,
useValue: featuresServiceMock,
},
],
});
pipe = new TagsPipe(featuresServiceMock, siteServiceMock);
});
it('should transform when # in the middle ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring#name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -32,7 +32,6 @@ describe('TagPipe', () => {
});
it('should transform when # preceded by space ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring #name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -41,7 +40,6 @@ describe('TagPipe', () => {
});
it('should transform when # preceded by [] ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring [#name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -50,7 +48,6 @@ describe('TagPipe', () => {
});
it('should transform when # preceded by () ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring (#name)';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -59,7 +56,6 @@ describe('TagPipe', () => {
});
it('should transform uppercase text following # to lower case ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textString #NaMe';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -68,7 +64,6 @@ describe('TagPipe', () => {
});
it('should correctly parse when duplicates substrings present', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = '#hash #hashlonger';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -80,28 +75,24 @@ describe('TagPipe', () => {
});
it('should transform when @ preceded by () ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring (@name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a class="tag"');
});
it('should transform when @ preceded by [] ', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring [@name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a class="tag"');
});
it('should transform when @ preceded by space', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring @name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a class="tag"');
});
it('should transform when @ followed by `.com`', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring @name.com';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a class="tag"');
......@@ -109,7 +100,6 @@ describe('TagPipe', () => {
});
it('should transform two adjacent tags', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = '@test1 @test2';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toEqual(
......@@ -118,7 +108,6 @@ describe('TagPipe', () => {
});
it('should transform many adjacent tags', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string =
'@test1 @test2 @test3 @test4 @test5 @test6 @test7 @test8 @test9 @test10 @test11 @test12 @test13 @test14 @test15';
const transformedString = pipe.transform(<any>string);
......@@ -135,14 +124,12 @@ describe('TagPipe', () => {
});
it('should transform to an email', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring@name.com';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a href="mailto:textstring@name.com"');
});
it('should not transform when @ not present', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring name';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toEqual(string);
......@@ -150,35 +137,30 @@ describe('TagPipe', () => {
});
it('should transform url http', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring http://minds.com/';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a href="http://minds.com/');
});
it('should transform url with https', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring https://minds.com/';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a href="https://minds.com/');
});
it('should transform url with ftp', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring ftp://minds.com/';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a href="ftp://minds.com/');
});
it('should transform url with file', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'textstring file://minds.com/';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain('<a href="file://minds.com/');
});
it('should transform url with a hashtag', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'text http://minds.com/#position';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -187,7 +169,6 @@ describe('TagPipe', () => {
});
it('should transform url with a hashtag and @', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = 'text http://minds.com/#position@some';
const transformedString = pipe.transform(<any>string);
expect(transformedString).toContain(
......@@ -196,7 +177,6 @@ describe('TagPipe', () => {
});
it('should transform many tags', () => {
const pipe = new TagsPipe(featuresServiceMock);
const string = `text http://minds.com/#position@some @name
@name1 #hash1#hash2 #hash3 ftp://s.com name@mail.com
`;
......
import { Pipe, Inject, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { FeaturesService } from '../../services/features.service';
import { SiteService } from '../services/site.service';
@Pipe({
name: 'tags',
......@@ -31,7 +32,11 @@ export class TagsPipe implements PipeTransform {
hash: {
rule: /(^|\s||)#(\w+)/gim,
replace: m => {
if (this.featureService.has('top-feeds')) {
if (this.siteService.isProDomain) {
return `${
m.match[1]
}<a href="/all;query=${m.match[2].toLowerCase()}">#${m.match[2]}</a>`;
} else if (this.featureService.has('top-feeds')) {
return `${
m.match[1]
}<a href="/newsfeed/global/top;hashtag=${m.match[2].toLowerCase()};period=24h">#${
......@@ -49,7 +54,10 @@ export class TagsPipe implements PipeTransform {
},
};
constructor(private featureService: FeaturesService) {}
constructor(
private featureService: FeaturesService,
private siteService: SiteService
) {}
/**
* Push a match to results array
......
......@@ -38,6 +38,7 @@ import { OverlayModalService } from '../../../services/ux/overlay-modal';
import { ChannelMode } from '../../../interfaces/entities';
import { ifStmt } from '@angular/compiler/src/output/output_ast';
import { ChannelModulesComponent } from '../modules/modules';
import { SiteService } from '../../../common/services/site.service';
describe('ChannelSidebar', () => {
let comp: ChannelSidebar;
......@@ -134,6 +135,14 @@ describe('ChannelSidebar', () => {
provide: OverlayModalService,
useValue: overlayModalServiceMock,
},
{
provide: SiteService,
useValue: MockService(SiteService, {
props: {
isProDomain: { get: () => false },
},
}),
},
],
}).compileComponents(); // compile template and css
}));
......
......@@ -50,6 +50,7 @@ import { BlockListService } from '../../../../../common/services/block-list.serv
import { ClientMetaService } from '../../../../../common/services/client-meta.service';
import { clientMetaServiceMock } from '../../../../../../tests/client-meta-service-mock.spec';
import { AutocompleteSuggestionsService } from '../../../../suggestions/services/autocomplete-suggestions.service';
import { SiteService } from '../../../../../common/services/site.service';
/* tslint:disable */
// START MOCKS
......@@ -492,6 +493,14 @@ describe('Activity', () => {
provide: AutocompleteSuggestionsService,
useValue: MockService(AutocompleteSuggestionsService),
},
{
provide: SiteService,
useValue: MockService(SiteService, {
props: {
isProDomain: { get: () => false },
},
}),
},
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents(); // compile template and css
......
import { Component, Directive, EventEmitter } from '@angular/core';
import { SiteService } from '../common/services/site.service';
export function Mock(opts: any = {}) {
return (
......@@ -12,57 +13,85 @@ export function Mock(opts: any = {}) {
}
export function MockComponent(options: Component, spies: string[] = []) {
let metadata: Component = {
const metadata: Component = {
selector: options.selector,
template: options.template || '',
inputs: options.inputs,
outputs: options.outputs,
};
let component = class _ {};
const component = class _ {};
if (options.outputs) {
for (let output of options.outputs) {
for (const output of options.outputs) {
component.prototype[output] = new EventEmitter<any>();
}
}
for (let spy of spies) {
for (const spy of spies) {
component.prototype[spy] = jasmine.createSpy(spy);
}
return Component(metadata)(component);
}
export function MockDirective(options: Directive, spies: string[] = []) {
let metadata: Directive = {
const metadata: Directive = {
selector: options.selector,
inputs: options.inputs,
outputs: options.outputs,
};
let directive = class _ {};
const directive = class _ {};
if (options.outputs) {
for (let output of options.outputs) {
for (const output of options.outputs) {
directive.prototype[output] = new EventEmitter<any>();
}
}
for (let spy of spies) {
for (const spy of spies) {
directive.prototype[spy] = jasmine.createSpy(spy);
}
return Directive(metadata)(directive);
}
export function MockService(obj: any, config: any = null) {
let spies = {};
const keys = Object.keys(obj.prototype);
for (let key of keys) {
const spies = {};
// spy properties first
const props = Object.getOwnPropertyNames(obj.prototype).filter(key => {
return (
Object.getOwnPropertyDescriptor(obj.prototype, key).get ||
Object.getOwnPropertyDescriptor(obj.prototype, key).set
);
});
const keys = Object.keys(obj.prototype).filter(
key => props.indexOf(key) === -1
);
for (const prop of props) {
const property = {
get: () => false,
set: () => {},
};
if (config && config.props && config.props[prop]) {
if (config.props[prop].get) {
property.get = config.props[prop].get;
}
if (config.props[prop].set) {
property.set = config.props[prop].set;
}
}
Object.defineProperty(spies, prop, property);
}
for (const key of keys) {
let value = null;
if (config && config[key]) {
value = config[key];
}
spies[key] = jasmine.createSpy(key).and.returnValue(value);
}
return new Proxy(
{ ...spies, _config: config },
{
get: (target, prop) => {
//if spy exists, return it
// if spy exists, return it
if (target.hasOwnProperty(prop.toString())) {
return target[prop];
} else if (prop.toString() === '$quoted$') {
......