Skip to content

Commit

Permalink
fix final issues
Browse files Browse the repository at this point in the history
  • Loading branch information
double-beep authored Jan 9, 2023
1 parent 0699f93 commit 4d17365
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
23 changes: 15 additions & 8 deletions lib/stackexchange.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type JQueryCss = JQuery.PlainObject<
string | number | ((this: Element, index: number, value: string) => string | number | void | undefined)
>;

type XHRMock = {
reponseText: string
};

declare global {
namespace StackExchange {
interface ModalType {
Expand Down Expand Up @@ -182,7 +186,7 @@ declare global {
}

interface DelayedReactionObject {
trigger: () => void;
trigger: (...args: any[]) => void;
cancel: () => void;
}

Expand Down Expand Up @@ -253,7 +257,10 @@ declare global {
element: HTMLElement,
jMenu: JQuery
) => void;
// TODO what does predicate() actually do?
/**
* A function that if when called returns `false`, the popup won't be shown
* @param element The original element
*/
predicate?: (element: HTMLElement) => void;
}

Expand Down Expand Up @@ -373,13 +380,13 @@ declare global {
* @param options Configuration
* @returns A `$.Deferred().promise()` that resolves to the jQuery modal element
*/
loadModal<T>(
loadModal(
url: string,
options?: Partial<ShowModalOptions> & {
/** Error message shown on request error */
defaultErrorMessage?: string
}
): ReturnType<JQueryDeferred<T>["promise"]>;
): ReturnType<JQueryDeferred<JQuery>["promise"]>;
/**
* Tells Stacks to show a popover by calling the stimulus controller directly
* @param $triggerElement The jQuery popover element
Expand Down Expand Up @@ -531,14 +538,14 @@ declare global {
* and not the full /error page's HTML.
* @param jqXHR http://api.jquery.com/jQuery.ajax/#jqXHR
*/
getLikelyErrorMessage(jqXHR: JQuery.jqXHR): string;
getLikelyErrorMessage(jqXHR: ReturnType<JQueryDeferred<XHRMock>["reject"]>): string;
/*
* For use with the `getLikelyErrorMessage()` method.
* Returns a rejected `.Deferred` with the first (and only) argument
* a mock XHR with `.responseText` property set to `errorMessage`.
* @param errorMessage Will be set to the resulting object's `.responseText` property
*/
getRejectedMockXhr(errorMessage: string): JQuery.DeferredStatic; // TODO FIXME rejected promise
getRejectedMockXhr(errorMessage: string): ReturnType<JQueryDeferred<XHRMock>["reject"]>;
/**
* When 'enter' is pressed while `$form`'s child textarea has focus, `$form` will be submitted.
*
Expand Down Expand Up @@ -643,7 +650,7 @@ declare global {
flags: number,
value: boolean,
userId?: number
): JQuery.DeferredStatic; // TODO FIXME
): ReturnType<ReturnType<JQueryStatic["post"]>["then"]>;
/**
* Toggles a particular preference on/off
* @param flags The flag/option id
Expand All @@ -654,7 +661,7 @@ declare global {
flags: number,
value: boolean,
accountId?: number
): JQuery.DeferredStatic; // TODO FIXME;
): ReturnType<ReturnType<JQueryStatic["post"]>["then"]>;
};

// NOTE: optional fields do not appear if the user has logged out
Expand Down
28 changes: 16 additions & 12 deletions spec/stackexchange.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ expectType<void>(helpers.removeMessages());
expectType<void>(helpers.removeMessages(true));

expectType<JQuery[]>([
await helpers.loadModal<JQuery>("/url"),
await helpers.loadModal<JQuery>(
await helpers.loadModal("/url"),
await helpers.loadModal(
"url",
{ returnElements: element }
),
Expand Down Expand Up @@ -225,10 +225,12 @@ expectType<void[]>([
]);
expectError(helpers.showFancyOverlay({}));

// TODO
/* DELAYEDREACTION - start */
/* ----------------------- */
/* DELAYEDREACTION - end */
const exampleFn = (str: string) => console.log(str);
const delayed = helpers.DelayedReaction(exampleFn, 100, { sliding: true });
expectType<void>(delayed.trigger("string"));
expectType<void>(delayed.cancel());
expectType<() => void>(delayed.cancel);
expectType<(...args: any[]) => void>(delayed.trigger);

expectType<void>(helpers.fireAndForget("url"));
expectError(helpers.fireAndForget());
Expand All @@ -253,9 +255,6 @@ expectError(helpers.parseUrl());
expectType<boolean>(helpers.isEmailAddress("[email protected]"));
expectError(helpers.isEmailAddress());

// TODO
/* getLikelyErrorMessage, getRejectedMockXhr */

expectType<void>(helpers.submitFormOnEnterPress(element));
expectError(helpers.submitFormOnEnterPress());

Expand Down Expand Up @@ -359,9 +358,14 @@ expectError(helpers.tagSeparator());
expectType<string>(helpers.sanitizeAndSplitTags(element.text(), true, false));
expectError(helpers.sanitizeAndSplitTags());

// TODO
/* toggleUserFlags, toggleAccountPreferenceFlags */
// TODO find what each POST request returns & insert types
// don't care about the types returned
// these should just avoid any errors
helpers.toggleUserFlags(0, true);
helpers.toggleUserFlags(0, false);
helpers.toggleAccountPreferenceFlags(0, true);
helpers.toggleAccountPreferenceFlags(0, false);
expectError(helpers.toggleUserFlags(0));
expectError(helpers.toggleAccountPreferenceFlags(0));

// end .helpers

Expand Down

0 comments on commit 4d17365

Please sign in to comment.