Skip to content

Commit

Permalink
refactor(downloads-list): improve components, move fixtures
Browse files Browse the repository at this point in the history
add tests for filtering
  • Loading branch information
fgravin committed Jun 22, 2022
1 parent c16b90b commit 1146c3a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { By } from '@angular/platform-browser'
import {
LINK_FIXTURES,
LinkHelperService,
MetadataLinkValid,
} from '@geonetwork-ui/util/shared'
import { TranslateModule } from '@ngx-translate/core'
import { LINK_FIXTURES } from '../../../../../util/shared/src/lib/links/link.fixtures'

import { DownloadsListComponent } from './downloads-list.component'

Expand All @@ -24,7 +24,7 @@ const linkHelperServiceMock = {
selector: 'gn-ui-download-item',
template: ``,
})
export class DownloadItemComponentMock {
class MockDownloadItemComponent {
@Input() link: MetadataLinkValid
@Input() color: string
}
Expand All @@ -37,7 +37,7 @@ describe('DownloadsListComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TranslateModule.forRoot()],
declarations: [DownloadsListComponent, DownloadItemComponentMock],
declarations: [DownloadsListComponent, MockDownloadItemComponent],
schemas: [NO_ERRORS_SCHEMA],
providers: [
{
Expand All @@ -63,16 +63,65 @@ describe('DownloadsListComponent', () => {
fixture.detectChanges()
expect(component).toBeTruthy()
})

describe('when a list of downloads', () => {
let items: DebugElement[]

beforeEach(() => {
component.links = [
LINK_FIXTURES.dataCsv,
LINK_FIXTURES.dataPdf,
LINK_FIXTURES.dataPdf,
]
fixture.detectChanges()
items = de.queryAll(By.directive(MockDownloadItemComponent))
})
it('contains one link', () => {
expect(items.length).toBe(3)
})
})
describe('when link format is unknown', () => {
let items: DebugElement[]

beforeEach(() => {
component.links = [LINK_FIXTURES.unknownFormat]
fixture.detectChanges()
items = de.queryAll(By.directive(DownloadItemComponentMock))
items = de.queryAll(By.directive(MockDownloadItemComponent))
})
it('contains one link', () => {
expect(items.length).toBe(1)
})
})
describe('hydrates link with color and format', () => {
let items: DebugElement[]

beforeEach(() => {
component.links = [LINK_FIXTURES.geodataShpWithMimeType]
fixture.detectChanges()
items = de.queryAll(By.directive(MockDownloadItemComponent))
})
it('contains color, isWfs & format', () => {
expect(items.length).toBe(1)
expect(items[0].componentInstance.link).toEqual({
...LINK_FIXTURES.geodataShpWithMimeType,
color: 'var(--color-gray-700)',
format: '',
isWfs: true,
})
})
})
describe('filtering', () => {
let items: DebugElement[]

beforeEach(() => {
component.links = [{ ...LINK_FIXTURES.dataCsv, format: 'csv' }]
component.activeFilterFormats = ['csv', 'json']
fixture.detectChanges()
})
it('csv link is displayed', () => {
expect(component.filteredLinks.length).toBe(1)
component.toggleFilterFormat('csv')
expect(component.filteredLinks.length).toBe(0)
})
})
})
29 changes: 12 additions & 17 deletions libs/ui/elements/src/lib/downloads-list/downloads-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ export class DownloadsListComponent implements OnInit {
}

ngOnInit(): void {
this.processedLinks = this.formatLinks(this.links)
this.processedLinks = this.assignColor(this.processedLinks)
this.processedLinks = this.isGeneratedFromWfs(this.processedLinks)
this.processedLinks = this.assignColor(this.links)
this.processedLinks = this.formatWfs(this.processedLinks)
this.filteredLinks = this.filterLinks(this.processedLinks)

this.filterButtons = this.filterFormats.map((format) => {
Expand All @@ -66,15 +65,6 @@ export class DownloadsListComponent implements OnInit {
})
}

formatLinks(links) {
return links.map((link) => {
return {
...link,
format: link.format ? link.format.split(':').at(-1) : '',
}
})
}

filterLinks(links: MetadataLinkValid[]) {
if (
this.activeFilterFormats.length === 0 ||
Expand Down Expand Up @@ -107,13 +97,18 @@ export class DownloadsListComponent implements OnInit {
})
}

isGeneratedFromWfs(links: MetadataLinkValid[]) {
formatWfs(links: MetadataLinkValid[]) {
return links.map((link) => {
if (!this.linkHelper.isWfsLink(link)) return link
return {
...link,
isWfs: true,
if (this.linkHelper.isWfsLink(link)) {
const { format = '' } = link
const tokens = format.split(':')
link = {
...link,
isWfs: true,
format: tokens[tokens.length - 1],
}
}
return link
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion libs/ui/elements/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"declarationMap": true,
"inlineSources": true,
"types": [],
"lib": ["dom", "es2018"]
"lib": ["dom", "es2022"]
},
"exclude": [
"src/test-setup.ts",
Expand Down
1 change: 1 addition & 0 deletions libs/util/shared/src/lib/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './geojson.fixtures'
export * from './ol-feature.fixture'
export * from './record-link.fixtures'
export * from './link.fixtures'
export * from './records'
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinkClassifierService, LinkUsage } from './link-classifier.service'
import { LINK_FIXTURES } from './link.fixtures'
import { LINK_FIXTURES } from '../fixtures/link.fixtures'

describe('LinkClassifierService', () => {
let service: LinkClassifierService
Expand Down
2 changes: 1 addition & 1 deletion libs/util/shared/src/lib/links/link-helper.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MetadataLink, MetadataRecord } from '../models'
import { LinkClassifierService, LinkUsage } from './link-classifier.service'

import { LinkHelperService } from './link-helper.service'
import { LINK_FIXTURES } from './link.fixtures'
import { LINK_FIXTURES } from '../fixtures/link.fixtures'

let linkUsage: LinkUsage[]
const linkClassifierMock = {
Expand Down
2 changes: 1 addition & 1 deletion libs/util/shared/src/lib/links/link-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
sortPriority,
FORMATS,
} from './link-utils'
import { LINK_FIXTURES } from './link.fixtures'
import { LINK_FIXTURES } from '../fixtures/link.fixtures'

jest.mock('@camptocamp/ogc-client', () => ({
WfsEndpoint: class {
Expand Down

0 comments on commit 1146c3a

Please sign in to comment.