Skip to content

Commit

Permalink
Release 5.11.15
Browse files Browse the repository at this point in the history
### Changelog:
- Feature(frontend): Export typed ``onAppCreated`` hook.
- Chore(frontend): Update npm dependencies.
- Chore(frontend): Remove unused eslint directives.

See merge request vst/vst-utils!679
  • Loading branch information
onegreyonewhite committed Nov 29, 2024
2 parents 87792cd + 289e749 commit 7246978
Show file tree
Hide file tree
Showing 48 changed files with 936 additions and 907 deletions.
1 change: 1 addition & 0 deletions frontend_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
hookViewOperation,
onAppAfterInit,
onAppBeforeInit,
onAppCreated,
onFilterListViewColumns,
onFilterOperations,
onRoutesCreated,
Expand Down
8 changes: 5 additions & 3 deletions frontend_src/unittests/create-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createLocalVue } from '@vue/test-utils';
import VueI18n from 'vue-i18n';
import { type AppSchema } from '#vstutils/schema';
import { type InitAppConfigRaw, _createUserManager, type UserProfile } from '#vstutils/init-app';
import { getApp } from '#vstutils/utils';
import { getApp, streamToString } from '#vstutils/utils';
import testSchema from '../__mocks__/testSchema.json';

// Important to import from index to trigger side effects as if it was a real app
Expand Down Expand Up @@ -56,9 +56,11 @@ export async function createApp(params?: { schema?: Partial<AppSchema>; disableB

// Start app

// eslint-disable-next-line @typescript-eslint/require-await
fetchMock.mockResponse(async (request) => {
const body = (request.body as Buffer | null)?.toString();
let body: undefined | string = undefined;
if (request.body) {
body = await streamToString(request.body);
}

if (
request.url === 'https://auth.test/.well-known/openid-configuration' &&
Expand Down
22 changes: 9 additions & 13 deletions frontend_src/unittests/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mount as vueMount, createWrapper, type Wrapper } from '@vue/test-utils';
import { waitFor as _waitFor, within } from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import { getApp } from '#vstutils/utils';
import { getApp, streamToString } from '#vstutils/utils';

import type { ComponentOptions } from 'vue';

Expand Down Expand Up @@ -49,27 +49,23 @@ export async function waitForPageLoading() {
await waitFor(() => expect(isLoading()).toBeFalsy());
}

export function expectRequest(
request: undefined | Request | [string | Request | undefined, RequestInit | undefined],
export async function expectRequest(
params: undefined | Parameters<typeof global.fetch>,
expected: { url?: string; method?: string; body: string | object; headers?: Record<string, string> },
) {
if (!request) {
if (!params) {
throw new Error('Request expected');
}
if (Array.isArray(request)) {
// @ts-expect-error It's actually a Request
request = new Request(...request);
}
const request = new Request(...params);
if (expected.url) {
expect(request.url).toBe(expected.url);
}
if (expected.method) {
expect(request.method).toBe(expected.method.toUpperCase());
expect(request.method.toUpperCase()).toBe(expected.method.toUpperCase());
}
if (expected.body) {
expect(request.body).toBeTruthy();
// @ts-expect-error It's actually a Buffer
const body = (request.body as Buffer).toString();
const body = request.body ? await streamToString(request.body) : '';
if (typeof expected.body === 'object') {
expect(JSON.parse(body)).toEqual(expected.body);
} else {
Expand All @@ -84,11 +80,11 @@ export function expectRequest(
}
}

export function expectNthRequest(
export async function expectNthRequest(
idx: number,
expected: { url?: string; method?: string; body: string | object; headers?: Record<string, string> },
) {
expectRequest(fetchMockCallAt(idx), expected);
await expectRequest(fetchMockCallAt(idx), expected);
}

export function fetchMockCallAt(idx: number) {
Expand Down
3 changes: 0 additions & 3 deletions frontend_src/unittests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ Vue.config.productionTip = false;
Vue.config.devtools = false;

// @ts-expect-error Mock canvas
// eslint-disable-next-line @typescript-eslint/no-empty-function
QRCode.toCanvas = () => {};

// @ts-expect-error Skip some properties
global.$ = global.jQuery = $;

// @ts-expect-error Mock adminlte jquery functions
// eslint-disable-next-line @typescript-eslint/no-empty-function
$.fn.PushMenu = () => {};

globalThis.IS_TESTS = true;
Expand All @@ -32,7 +30,6 @@ fetchMocker.enableMocks();

if (typeof window.URL.createObjectURL === 'undefined') {
// @ts-expect-error Skip some properties
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
window.URL.createObjectURL = vi.fn(() => globalThis.OBJECT_URL);
}

Expand Down
2 changes: 0 additions & 2 deletions frontend_src/vstutils/AppRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@
},
props: {
info: { type: Object, required: true },
// eslint-disable-next-line vue/prop-name-casing
x_menu: { type: Array as PropType<XMenu>, required: true },
// eslint-disable-next-line vue/prop-name-casing
x_docs: { type: Object, required: true },
},
setup() {
Expand Down
4 changes: 2 additions & 2 deletions frontend_src/vstutils/__tests__/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Actions', () => {

await app.actions.execute({ action });
expect(fetchMock.mock.calls).toHaveLength(1);
expectNthRequest(0, {
await expectNthRequest(0, {
method: 'PUT',
url: 'http://localhost/api/endpoint/',
body: [{ method: 'put', path: '/test/path/' }],
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Actions', () => {
});
expect(result.data).toEqual({ some: 'return val' });
expect(fetchMock).toBeCalledTimes(1);
expectNthRequest(0, {
await expectNthRequest(0, {
method: 'PUT',
path: '/api/endpoint/',
body: [{ method: 'patch', data: { testField: 'some val' }, path: 'execute' }],
Expand Down
4 changes: 1 addition & 3 deletions frontend_src/vstutils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export class ActionsManager {
return this.app.router.push(formatPath(path, this.app.router.currentRoute.params, instance));
}

// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Cannot execute action ${action.name} on instance ${instance}`);
}

Expand Down Expand Up @@ -169,8 +168,7 @@ export class ActionsManager {
}
}
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const str = this.app.error_handler.errorToString(error) as string;
const str = this.app.error_handler.errorToString(error);

const srt_to_show = i18n.ts(pop_up_msg.instance.error.executeEmpty, [
i18n.t(action.name),
Expand Down
11 changes: 0 additions & 11 deletions frontend_src/vstutils/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ export class App implements IApp {
afterInitialDataBeforeMount() {
this.generateDefinitionsModels();

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this.views = new ViewConstructor(undefined, this.modelsResolver, this.fieldsResolver).generateViews(
this.schema,
);
Expand Down Expand Up @@ -328,10 +327,8 @@ export class App implements IApp {
const models = new Set<ModelConstructor>();

if (view.objects) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
for (const m of Object.values(view.objects.models)) {
if (Array.isArray(m)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
for (const model of m) models.add(model);
} else {
models.add(m!);
Expand All @@ -347,7 +344,6 @@ export class App implements IApp {
for (const model of models) {
if (model) {
for (const field of model.fields.values()) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
field.prepareFieldForView(path);
}
}
Expand All @@ -363,7 +359,6 @@ export class App implements IApp {
// Call hook for detail view filters
if (view instanceof PageView && view.filtersModelClass) {
for (const field of view.filtersModelClass.fields.values()) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
field.prepareFieldForView(path);
}
}
Expand All @@ -377,7 +372,6 @@ export class App implements IApp {
const modelName = listView.objects.getResponseModelClass(utils.RequestTypes.LIST)!.name;
try {
// @ts-expect-error TODO refactor qs resolver to ts
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
listView.nestedQueryset = this.qsResolver.findQuerySetForNested(modelName, listView.path);
} catch (e) {
console.warn(e);
Expand Down Expand Up @@ -422,7 +416,6 @@ export class App implements IApp {
this.localSettingsStore = createLocalSettingsStore(
window.localStorage,
'localSettings',
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
this.localSettingsModel as ModelConstructor,
)(pinia);
this.localSettingsStore.load();
Expand All @@ -447,12 +440,10 @@ export class App implements IApp {
// in tests, so use router.constructor instead.
Vue.use(this.router.constructor);

/* eslint-disable */
Vue.prototype.$app = this;
Vue.prototype.$u = utils;
Vue.prototype.$st = i18n.st.bind(i18n);
Vue.prototype.$ts = i18n.ts.bind(i18n);
/* eslint-enable */

this.rootVm = new Vue({
mixins: [this.appRootComponent, ...this.additionalRootMixins],
Expand Down Expand Up @@ -485,15 +476,13 @@ export class App implements IApp {
initActionConfirmationModal({ title }: { title: string }): Promise<void> {
return new Promise((resolve) => {
if (this.rootVm?.appModals) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
this.rootVm.appModals!.initActionConfirmationModal(() => resolve(), title);
}
});
}

openReloadPageModal() {
if (this.rootVm?.appModals) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
this.rootVm.appModals!.openReloadPageModal();
}
}
Expand Down
1 change: 0 additions & 1 deletion frontend_src/vstutils/autoupdate/AutoUpdateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ export class AutoUpdateController {
clearTimeout(this.timeoutId);
}

// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.timeoutId = setTimeout(async () => {
if (Visibility.state() !== 'hidden') {
await this.updateTimerData();
Expand Down
1 change: 0 additions & 1 deletion frontend_src/vstutils/autoupdate/useAutoUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
TimerAutoUpdateAction,
} from './AutoUpdateController';

// eslint-disable-next-line @typescript-eslint/no-empty-function
async function EMPTY_CALLBACK() {}

export function useAutoUpdate({
Expand Down
4 changes: 2 additions & 2 deletions frontend_src/vstutils/components/items/sidebar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export interface MenuItem {

export function openSidebar() {
// @ts-expect-error AdminLTE has no types
// eslint-disable-next-line @typescript-eslint/no-unsafe-call

$('[data-widget="pushmenu"]').PushMenu('expand');
}

export function hideSidebar() {
if (document.body.classList.contains('sidebar-open')) {
// @ts-expect-error AdminLTE has no types
// eslint-disable-next-line @typescript-eslint/no-unsafe-call

$('[data-widget="pushmenu"]').PushMenu('collapse');
}
}
Expand Down
1 change: 0 additions & 1 deletion frontend_src/vstutils/fetch-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ function fetchInstancesFields(instances: Model[], fields: Field[], options?: Opt
} else if (field instanceof ArrayField) {
promises.push(fetchArrayFieldValues(field as ArrayField, instances, options));
} else if (field instanceof DynamicField) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
promises.push(fetchDynamicFieldValues(field, instances, options));
} else if (field instanceof RelatedListField) {
promises.push(fetchRelatedListFieldValues(field, instances, options));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
this.onUpdateInstances();
this.loadInstances();
},
// eslint-disable-next-line @typescript-eslint/no-empty-function
onUpdateInstances() {},
/**
* Method, that generates filters for qs.
Expand Down
13 changes: 5 additions & 8 deletions frontend_src/vstutils/fields/FieldsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ export class FieldsResolver {
}

_resolveJsonPointer(pointer: string): unknown {
return (
pointer
.split('/')
.slice(1)
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
.reduce((obj, fragment) => obj[fragment], this._schema)
);
return pointer
.split('/')
.slice(1)

.reduce((obj, fragment) => obj[fragment], this._schema);
}

registerField(
Expand Down Expand Up @@ -101,7 +99,6 @@ export class FieldsResolver {

const typeMap = this._types.get(obj.type!);
if (!typeMap) {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
console.warn(`Unknown type: ${obj.type}`);
return this._defaultField(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
function openSidebar(e: Event) {
e.stopPropagation();
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
app.rootVm.openControlSidebar();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe('Barcode fields', () => {
await nextTick();

// after clicking input must appear
// eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style

const editManuallyInput = qrcodeFieldWrapperEl?.querySelector(
'input[type="text"]',
) as HTMLInputElement;
Expand All @@ -259,11 +259,11 @@ describe('Barcode fields', () => {
expect(scannerCameraSelectEl?.getAttribute('disabled')).toBeTruthy();

// check that qrcode field editing updates sandbox
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access

expect(app.store.page.sandbox.qrcode).toBe('qrcode');
fireEvent.input(editManuallyInput, { target: { value: 'some new value' } });
expect(editManuallyInput.value).toBe('some new value');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access

expect(app.store.page.sandbox.qrcode).toBe('some new value');
});
});
4 changes: 1 addition & 3 deletions frontend_src/vstutils/fields/base/BaseField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class BaseField<Inner, Represent, XOptions extends DefaultXOptions = Defa
* Method, that prepares instance of field for usage. Method is called after models and views are
* created, for every field instance that is part of view.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function

prepareFieldForView(path: string): void {}

/**
Expand Down Expand Up @@ -261,7 +261,6 @@ export class BaseField<Inner, Represent, XOptions extends DefaultXOptions = Defa
return value;
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
validateInner(data: InnerData): Inner | null | undefined {
return this.getValue(data);
}
Expand Down Expand Up @@ -294,7 +293,6 @@ export class BaseField<Inner, Represent, XOptions extends DefaultXOptions = Defa
* Method that creates property descriptor from current field
*/
toDescriptor(): ModelPropertyDescriptor<Represent> {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const fieldThis = this;

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
mixins: [BaseFieldContentMixin, BaseFieldInnerComponentMixin, FieldLabelIdMixin],
props: {
field: { type: Object as PropType<Field>, required: true },
// eslint-disable-next-line vue/require-prop-types
value: { default: undefined },
data: { type: Object, required: true },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
const validIds = computed(() => enumItems.value.map((value) => value.id));
// eslint-disable-next-line no-undef
function handleChange(data: SelectedData[], event: JQuery.ChangeEvent) {
let value;
const selected = data[0];
Expand Down
1 change: 0 additions & 1 deletion frontend_src/vstutils/fields/dynamic/DependFromFkField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class DependFromFkField extends DynamicField<XOptions> {
}

static get mixins() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return [DependFromFkFieldMixin as any];
}

Expand Down
Loading

0 comments on commit 7246978

Please sign in to comment.