feat: resolvers for chain edit and list
This commit is contained in:
58
src/app/resolvers/chain-resolver.spec.ts
Normal file
58
src/app/resolvers/chain-resolver.spec.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { ActivatedRoute, provideRouter, RedirectCommand, ResolveFn, Router } from '@angular/router';
|
||||
|
||||
import { chainResolver } from './chain-resolver';
|
||||
import { Chain } from '../models/Chain';
|
||||
import { Component } from '@angular/core';
|
||||
import { ChainDAO } from '../dao/ChainDAO';
|
||||
|
||||
@Component({
|
||||
selector: 'app-mock',
|
||||
template: ``,
|
||||
styles: ``,
|
||||
})
|
||||
class MockComponent {}
|
||||
describe('chainResolver', () => {
|
||||
let activatedRoute: ActivatedRoute;
|
||||
let chainDAO: Partial<ChainDAO>;
|
||||
let router: Router;
|
||||
|
||||
let CHAIN_MOCK: Chain;
|
||||
const executeResolver: ResolveFn<Chain> = (...resolverParameters) =>
|
||||
TestBed.runInInjectionContext(() => chainResolver(...resolverParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
chainDAO = {
|
||||
findBy: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
CHAIN_MOCK = new Chain('mock', 'mock_img.jpeg', 1);
|
||||
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideRouter([
|
||||
{ path: 'mock/:id', component: MockComponent, resolve: { chain: chainResolver } },
|
||||
{ path: 'settings/chains', component: MockComponent },
|
||||
]),
|
||||
{ provide: ChainDAO, useValue: chainDAO },
|
||||
],
|
||||
});
|
||||
|
||||
activatedRoute = TestBed.inject(ActivatedRoute);
|
||||
router = TestBed.inject(Router);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeResolver).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should navigate back to settings for not found ids', async () => {
|
||||
await router.navigate(['mock', '99']);
|
||||
expect(router.url).toEqual('/settings/chains');
|
||||
});
|
||||
|
||||
it('should complete navigation for found ids', async () => {
|
||||
chainDAO.findBy = vi.fn().mockResolvedValue([CHAIN_MOCK]);
|
||||
await router.navigate(['mock', String(CHAIN_MOCK.id)]);
|
||||
expect(router.url).toEqual(`/mock/${CHAIN_MOCK.id}`);
|
||||
});
|
||||
});
|
||||
24
src/app/resolvers/chain-resolver.ts
Normal file
24
src/app/resolvers/chain-resolver.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { RedirectCommand, ResolveFn, Router } from '@angular/router';
|
||||
import { Chain } from '../models/Chain';
|
||||
import { ChainDAO } from '../dao/ChainDAO';
|
||||
import { inject } from '@angular/core';
|
||||
|
||||
export const chainResolver: ResolveFn<Chain> = async (route, _) => {
|
||||
const chainDAO = inject(ChainDAO);
|
||||
const router = inject(Router);
|
||||
const chainID = (<{ id: string }>route.params).id;
|
||||
let chain = new Chain('', null);
|
||||
try {
|
||||
const results = await chainDAO.findBy({ id: Number(chainID) });
|
||||
if (!results[0]) {
|
||||
throw new Error('The search for chain on edit did not find any match');
|
||||
}
|
||||
chain = results[0];
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return new RedirectCommand(router.parseUrl('settings/chains'), {
|
||||
skipLocationChange: true,
|
||||
});
|
||||
}
|
||||
return chain;
|
||||
};
|
||||
47
src/app/resolvers/chains-resolver.spec.ts
Normal file
47
src/app/resolvers/chains-resolver.spec.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideRouter, ResolveFn, Router } from '@angular/router';
|
||||
|
||||
import { chainsResolver } from './chains-resolver';
|
||||
import { Chain } from '../models/Chain';
|
||||
import { Component } from '@angular/core';
|
||||
import { ChainDAO } from '../dao/ChainDAO';
|
||||
|
||||
@Component({
|
||||
selector: 'app-mock',
|
||||
template: ``,
|
||||
styles: ``,
|
||||
})
|
||||
class MockComponent {}
|
||||
|
||||
describe('chainResolver', () => {
|
||||
const executeResolver: ResolveFn<Chain[]> = (...resolverParameters) =>
|
||||
TestBed.runInInjectionContext(() => chainsResolver(...resolverParameters));
|
||||
|
||||
let chainDAO: Partial<ChainDAO>;
|
||||
let router: Router;
|
||||
|
||||
beforeEach(() => {
|
||||
chainDAO = {
|
||||
findAll: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
provideRouter([
|
||||
{ path: 'chains', component: MockComponent, resolve: { chains: chainsResolver } },
|
||||
]),
|
||||
{ provide: ChainDAO, useValue: chainDAO },
|
||||
],
|
||||
});
|
||||
|
||||
router = TestBed.inject(Router);
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeResolver).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should call chaiDAO findAll method', async () => {
|
||||
await router.navigate(['chains']);
|
||||
expect(chainDAO.findAll).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
15
src/app/resolvers/chains-resolver.ts
Normal file
15
src/app/resolvers/chains-resolver.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { inject } from '@angular/core';
|
||||
import { ResolveFn } from '@angular/router';
|
||||
import { ChainDAO } from '../dao/ChainDAO';
|
||||
import { Chain } from '../models/Chain';
|
||||
|
||||
export const chainsResolver: ResolveFn<Chain[]> = async (route, state) => {
|
||||
const chainDAO = inject(ChainDAO);
|
||||
let chains: Chain[] = [];
|
||||
try {
|
||||
chains = await chainDAO.findAll();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
return chains;
|
||||
};
|
||||
Reference in New Issue
Block a user