Skip to content

Commit

Permalink
Merge branch 'Api_telechargement' into datahub-ign-2905
Browse files Browse the repository at this point in the history
  • Loading branch information
mmohadIGN authored Jul 4, 2024
2 parents 5295533 + f1f0af2 commit 2a50234
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('RecordApisComponent', () => {
expect(component.selectedApiLink).toEqual(serviceDistributionMock)
})
it('should update maxHeight for transition', () => {
expect(component.maxHeight).toEqual('500px')
expect(component.maxHeight).toEqual('700px')
})
it('should update opacity for transition', () => {
expect(component.opacity).toEqual(1)
Expand Down
169 changes: 115 additions & 54 deletions libs/feature/record/src/lib/ign-api-dl/ign-api-dl.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { TestBed, ComponentFixture } from '@angular/core/testing'
import { RecordApiFormComponent } from './record-api-form.component'

import { DatasetServiceDistribution } from '@geonetwork-ui/common/domain/model/record'
import { firstValueFrom } from 'rxjs'
import { UiInputsModule } from '@geonetwork-ui/ui/inputs'
import { Choice, UiInputsModule } from '@geonetwork-ui/ui/inputs'
import { TranslateModule } from '@ngx-translate/core'
import { IgnApiDlComponent } from './ign-api-dl.component'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import exp from 'constants'

const mockDatasetServiceDistribution: DatasetServiceDistribution = {
url: new URL('https://api.example.com/data'),
type: 'service',
accessServiceProtocol: 'ogcFeatures',
accessServiceProtocol: 'GPFDL',
}

describe('RecordApFormComponent', () => {
let component: RecordApiFormComponent
let fixture: ComponentFixture<RecordApiFormComponent>
describe('IgnApiDlComponent', () => {
let component: IgnApiDlComponent
let fixture: ComponentFixture<IgnApiDlComponent>

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [RecordApiFormComponent],
imports: [UiInputsModule, TranslateModule.forRoot()],
declarations: [IgnApiDlComponent],
imports: [
UiInputsModule,
HttpClientTestingModule,
TranslateModule.forRoot(),
],
}).compileComponents()

fixture = TestBed.createComponent(RecordApiFormComponent)
fixture = TestBed.createComponent(IgnApiDlComponent)
component = fixture.componentInstance
component.apiLink = mockDatasetServiceDistribution
fixture.detectChanges()
})

it('should create', () => {
Expand All @@ -33,58 +39,113 @@ describe('RecordApFormComponent', () => {
describe('When panel is opened', () => {
it('should set the links and initial values correctly', async () => {
expect(component.apiBaseUrl).toBe('https://api.example.com/data')
expect(component.offset$.getValue()).toBe('')
expect(component.limit$.getValue()).toBe('')
expect(component.format$.getValue()).toBe('json')
expect(component.format$.getValue()).toBe('')
expect(component.zone$.getValue()).toBe('')
expect(component.crs$.getValue()).toBe('')
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe('https://api.example.com/data?f=json')
expect(url).toBe('https://api.example.com/data?pageSize=200&page=0')
//expect(component.apiLink.accessServiceProtocol).toBe('GPFDL')
})
})
describe('When URL params are changed', () => {
it('should update query URL correctly when setting offset, limit, and format', async () => {
const mockOffset = '10'
const mockLimit = '20'
const mockFormat = 'json'
component.setOffset(mockOffset)
component.setLimit(mockLimit)
component.setFormat(mockFormat)
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data?offset=${mockOffset}&limit=${mockLimit}&f=${mockFormat}`
)
describe('When Format changed', () => {
const bucketFormat: Choice[] = [
{ value: 'SHP', label: 'SHP' },
{ value: 'json', label: 'json' },
{ value: 'SQL', label: 'SQL' },
]
const mockFormat = 'SHP'
const mockBadFormat = 'notAFormat'
it('should be a correct format', async () => {
jest.spyOn(component, 'resetPage')
component.bucketPromisesFormat = bucketFormat
component.setFormat(mockFormat)
expect(component.format$.getValue()).toBe(mockFormat)
expect(component.resetPage).toHaveBeenCalled()
})

it('should not be a correct format', async () => {
component.bucketPromisesFormat = bucketFormat
component.setFormat(mockBadFormat)
expect(component.format$.getValue()).toBe('')
})
})
it('should remove the param in url if value is null', async () => {
const mockOffset = null
const mockLimit = '20'
const mockFormat = 'json'
component.setOffset(mockOffset)
component.setLimit(mockLimit)
component.setFormat(mockFormat)
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data?limit=${mockLimit}&f=${mockFormat}`
)
describe('When CRS changed', () => {
const bucketCRS: Choice[] = [
{ value: 'CRS12', label: 'CRS1' },
{ value: 'CRS2', label: 'CRS2' },
{ value: 'CRS3', label: 'CRS3' },
]
const mockCRS = 'CRS12'
const mockBadCRS = 'notACRS'
it('should be a correct CRS', async () => {
jest.spyOn(component, 'resetPage')
component.bucketPromisesCrs = bucketCRS
component.setCrs(mockCRS)
expect(component.crs$.getValue()).toBe(mockCRS)
expect(component.resetPage).toHaveBeenCalled()
})

it('should not be a correct CRS', async () => {
component.bucketPromisesCrs = bucketCRS
component.setCrs(mockBadCRS)
expect(component.crs$.getValue()).toBe('')
})
})
it('should remove the param in url if value is zero', async () => {
const mockOffset = '10'
const mockLimit = '0'
const mockFormat = 'json'
component.setOffset(mockOffset)
component.setLimit(mockLimit)
component.setFormat(mockFormat)
const url = await firstValueFrom(component.apiQueryUrl$)
expect(url).toBe(
`https://api.example.com/data?offset=${mockOffset}&f=${mockFormat}`
)
describe('When Zone changed', () => {
const bucketZone: Choice[] = [
{ value: 'D01', label: 'D01' },
{ value: 'D02', label: 'D02' },
{ value: 'D03', label: 'D03' },
]
const mockZone = 'D03'
const mockBadZone = 'notAZone'
it('should be a correct Zone', async () => {
jest.spyOn(component, 'resetPage')
component.bucketPromisesZone = bucketZone
component.setZone(mockZone)
expect(component.zone$.getValue()).toBe(mockZone)
expect(component.resetPage).toHaveBeenCalled()
})

it('should not be a correct Zone', async () => {
component.bucketPromisesZone = bucketZone
component.setZone(mockBadZone)
expect(component.zone$.getValue()).toBe('')
})
})

describe('When EditionDate changed', () => {
const mockEditionDate = '2022-04-30'
const mockBadEditionDate = '88-88-88'
it('hould be a correct edition date', () => {
jest.spyOn(component, 'resetPage')
component.setEditionDate(mockEditionDate)
expect(component.editionDate$.getValue()).toBe(mockEditionDate)
expect(component.resetPage).toHaveBeenCalled()
})
it('should not be a correct edition date', () => {
component.setEditionDate(mockBadEditionDate)
expect(component.editionDate$.getValue()).toBe('')
})
})

describe('When Url is reset', () => {
it('Should reset zone, format, crs, page and size value', () => {
component.resetUrl()
expect(component.zone$.getValue()).toBe('null')
expect(component.format$.getValue()).toBe('null')
expect(component.crs$.getValue()).toBe('null')
expect(component.page$.getValue()).toBe('0')
expect(component.size$.getValue()).toBe(component.initialPageSize)
})
})
})

describe('#resetUrl', () => {
it('should reset URL to default parameters', () => {
component.resetUrl()
expect(component.offset$.getValue()).toBe('')
expect(component.limit$.getValue()).toBe('')
expect(component.format$.getValue()).toBe('json')
describe('When page is reset', () => {
it('Should reset page value', () => {
component.resetPage()
expect(component.page$.getValue()).toBe('0')
})
})
})
})
26 changes: 18 additions & 8 deletions libs/feature/record/src/lib/ign-api-dl/ign-api-dl.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,33 @@ export class IgnApiDlComponent implements OnInit {
}

setEditionDate(value: string) {
this.editionDate$.next(value)
this.resetPage()
if (value.match(/[0-9]{4}-[0-1][0-9]-[0-3][0-9]/)) {
this.editionDate$.next(value)
this.resetPage()
}
}

setZone(value: string) {
this.zone$.next(value)
this.resetPage()
if (this.bucketPromisesZone.map((choice) => choice.value).includes(value)) {
this.zone$.next(value)
this.resetPage()
}
}

setCrs(value: string) {
this.crs$.next(value)
this.resetPage()
if (this.bucketPromisesCrs.map((choice) => choice.value).includes(value)) {
this.crs$.next(value)
this.resetPage()
}
}

setFormat(value: string) {
this.format$.next(value)
this.resetPage()
if (
this.bucketPromisesFormat.map((choice) => choice.value).includes(value)
) {
this.format$.next(value)
this.resetPage()
}
}

resetUrl() {
Expand Down
4 changes: 2 additions & 2 deletions libs/feature/record/src/lib/state/mdview.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export class MdViewFacade {
links
.filter((link) => this.linkClassifier.hasUsage(link, LinkUsage.API))
.sort((dd1, dd2) => {
return (dd2 as DatasetServiceDistribution).accessServiceProtocol ==
return (dd2 as DatasetServiceDistribution).accessServiceProtocol ===
'GPFDL'
? 1
: 0
: -1
})
)
)
Expand Down

0 comments on commit 2a50234

Please sign in to comment.