Skip to content

Commit

Permalink
test(portal-next): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdiw committed Jan 21, 2025
1 parent d450a5b commit 546e4b3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
11 changes: 9 additions & 2 deletions gravitee-apim-portal-webui-next/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import 'jest-preset-angular/setup-jest';
import '@angular/localize/init';
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';

setupZoneTestEnv();

// mocking swagger-ui for tests as it contains some JS incompatible with Jest
// This means we will not be able to test Swagger in our components tests
jest.mock('swagger-ui', () => jest.fn(() => ({ initOAuth: () => jest.fn() })));

// Set the mock date globally for all tests
const MOCK_DATE = new Date(1466424490000); // UTC Time: Mon Jun 20 2016 12:08:10.000
jest.useFakeTimers({ advanceTimers: true }).setSystemTime(MOCK_DATE);

// Need to define advanceTimers for async calls
jest.useFakeTimers({ advanceTimers: 1, now: MOCK_DATE }); // advance 1ms every 10ms

// Mock Date.now() so that it always returns the same date
Date.now = jest.fn(() => MOCK_DATE.getTime());
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ describe('ApplicationLogTableComponent', () => {
});

function expectGetApplicationLogs(logsResponse: LogsResponse, page: number = 1, query?: string, to?: number, from?: number) {
const toInMilliseconds = to ?? new Date().getTime();
const toInMilliseconds = to ?? Date.now();
const fromInMilliseconds = from ?? toInMilliseconds - 86400000;
httpTestingController
.expectOne(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, effect, input, OnInit, output } from '@angular/core';
import { Component, DestroyRef, effect, inject, input, OnInit, output } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
Expand All @@ -33,14 +34,16 @@ export class SearchBarComponent implements OnInit {
searchTerm = output<string>();
searchControl: FormControl<string> = new FormControl<string>(``, { nonNullable: true });

private destroyRef = inject(DestroyRef);

constructor() {
effect(() => {
this.searchControl.setValue(this.searchParam());
});
}

ngOnInit() {
this.searchControl.valueChanges.pipe(debounceTime(300), distinctUntilChanged()).subscribe(term => {
this.searchControl.valueChanges.pipe(debounceTime(300), distinctUntilChanged(), takeUntilDestroyed(this.destroyRef)).subscribe(term => {
this.searchTerm.emit(term);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ export class ApplicationLogService {
paramsList.push(`page=${params.page ?? 1}`);
paramsList.push(`size=${params.size ?? 10}`);

const currentDate = new Date();
const currentDate = Date.now();
const yesterdayDate = new Date();
yesterdayDate.setDate(yesterdayDate.getDate() - 1);

paramsList.push(`from=${params.from ?? yesterdayDate.getTime()}`);
paramsList.push(`to=${params.to ?? currentDate.getTime()}`);
paramsList.push(`to=${params.to ?? currentDate}`);
paramsList.push(`order=${params.order ?? 'DESC'}`);

paramsList.push(`field=${params.field ?? '@timestamp'}`);
Expand Down

0 comments on commit 546e4b3

Please sign in to comment.