Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for doc creation #348

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions web/app/components/new/doc-form.hbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{{! @glint-nocheck: not typesafe yet }}
{{#if this.docIsBeingCreated}}
<div class="hds-typography-display-400 mt-3 text-center">
<div
data-test-creating-new-doc
class="hds-typography-display-400 mt-3 text-center"
>
<FlightIcon @name="loading" @size="24" />
<div class="mt-8 text-display-200 font-semibold">
Creating draft in Google Drive...
Expand All @@ -10,6 +12,7 @@
</div>
{{else}}
<form
data-test-new-doc-form
class="grid grid-cols-[1fr_250px] grid-rows-1 gap-10"
{{on "submit" this.submit}}
{{did-insert this.registerForm}}
Expand All @@ -21,6 +24,7 @@
</div>
<div class="space-y-7 pt-10">
<Hds::Form::TextInput::Field
data-test-title-input
@type="text"
@isRequired={{true}}
@value={{this.title}}
Expand All @@ -37,6 +41,7 @@

<div class="relative">
<Hds::Form::Textarea::Field
data-test-summary-input
@value={{this.summary}}
rows="3"
name="summary"
Expand Down Expand Up @@ -74,43 +79,6 @@
/>
</div>

{{! Note: We are still refining the subscribe/follow feature set.
As part of that effort we will be looking into how the concept
of "tags" would be useful. For now, we are choosing to
comment out defining tags as part of the document draft
creation workflow.
}}
{{!-- <Hds::Form::Field @layout="vertical" @isOptional={{true}} as |F|>
{{yield
(hash
Error=F.Error
HelperText=F.HelperText
Label=F.Label
isRequired=F.isRequired
isOptional=F.isOptional
)
}}
<F.Control>
<Inputs::TagSelect
@selected={{this.tags}}
@onChange={{this.updateTags}}
/>
</F.Control>
<F.Label><FlightIcon @name="tag" />
Add tags
</F.Label>
{{#if this.formErrors.tags}}
<F.Error as |E|>
<E.Message>{{this.formErrors.tags}}</E.Message>
</F.Error>
{{/if}}
<F.HelperText>
Use tags to help people discover this document based on their
cross-functional interests. For instance, "raft", "design" or
"a11y". There is a maximum of 5 tags.
</F.HelperText>
</Hds::Form::Field> --}}

<Hds::Form::Field @layout="vertical" as |F|>
{{yield
(hash
Expand Down Expand Up @@ -157,6 +125,7 @@
@owner={{this.authenticatedUser.info.email}}
/>
<Hds::Button
data-test-create-button
@text="Create draft in Google Drive"
type="submit"
disabled={{not this.formRequirementsMet}}
Expand Down
19 changes: 6 additions & 13 deletions web/app/components/new/doc-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ interface DocFormErrors {
title: string | null;
summary: string | null;
productAbbreviation: string | null;
tags: string | null;
contributors: string | null;
}

const FORM_ERRORS: DocFormErrors = {
title: null,
summary: null,
productAbbreviation: null,
tags: null,
contributors: null,
};

Expand All @@ -48,7 +46,7 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi

@tracked protected title: string = "";
@tracked protected summary: string = "";
@tracked protected productArea: string | null = null;
@tracked protected productArea?: string;
@tracked protected productAbbreviation: string | null = null;
@tracked protected contributors: HermesUser[] = [];

Expand Down Expand Up @@ -118,14 +116,7 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi
* Validates the form and updates the `formErrors` property.
*/
private validate() {
const errors = { ...FORM_ERRORS };
if (this.productAbbreviation) {
if (/\d/.test(this.productAbbreviation)) {
errors.productAbbreviation =
"Product abbreviation can't include a number";
}
}
this.formErrors = errors;
this.formErrors = { ...FORM_ERRORS };
}

/**
Expand Down Expand Up @@ -175,7 +166,7 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi

@action protected onProductSelect(
productName: string,
attributes: ProductArea
attributes: ProductArea,
) {
this.productArea = productName;
this.productAbbreviation = attributes.abbreviation;
Expand Down Expand Up @@ -224,14 +215,16 @@ export default class NewDocFormComponent extends Component<NewDocFormComponentSi
// Set modal on a delay so it appears after transition.
this.modalAlerts.setActive.perform(
"draftCreated",
AWAIT_DOC_CREATED_MODAL_DELAY
AWAIT_DOC_CREATED_MODAL_DELAY,
);

this.router.transitionTo("authenticated.document", doc.id, {
queryParams: { draft: true },
});
} catch (err: unknown) {
this.docIsBeingCreated = false;

// TODO: Improve error handling.
this.flashMessages.add({
title: "Error creating document draft",
message: `${err}`,
Expand Down
42 changes: 31 additions & 11 deletions web/mirage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function (mirageConfig) {
return new Response(
200,
{},
{ facetHits: [{ value: facetMatch.attrs.product }] }
{ facetHits: [{ value: facetMatch.attrs.product }] },
);
} else {
return new Response(200, {}, { facetHits: [] });
Expand Down Expand Up @@ -131,7 +131,7 @@ export default function (mirageConfig) {
return new Response(
200,
{},
{ hits: schema.document.all().models }
{ hits: schema.document.all().models },
);
}
} else {
Expand Down Expand Up @@ -213,6 +213,22 @@ export default function (mirageConfig) {
return new Response(200, {}, matches.models);
});

/**
* Called when a user creates a new document.
*/
this.post("/drafts", (schema, request) => {
const document = schema.document.create({
...JSON.parse(request.requestBody),
});

document.update({
objectID: document.id,
owners: ["[email protected]"],
});

return new Response(200, {}, document.attrs);
});

/**
* Used when publishing a draft for review.
* Updates the document's status and isDraft properties.
Expand Down Expand Up @@ -282,7 +298,7 @@ export default function (mirageConfig) {
support_link_url: TEST_SUPPORT_URL,
version: "1.2.3",
short_revision: "abc123",
}
},
);
});

Expand Down Expand Up @@ -375,7 +391,9 @@ export default function (mirageConfig) {
return new Response(
200,
{},
schema.document.findBy({ objectID: request.params.document_id }).attrs
schema.document.findBy({
objectID: request.params.document_id,
}).attrs,
);
});

Expand All @@ -386,7 +404,9 @@ export default function (mirageConfig) {
return new Response(
200,
{},
schema.document.findBy({ objectID: request.params.document_id }).attrs
schema.document.findBy({
objectID: request.params.document_id,
}).attrs,
);
});

Expand Down Expand Up @@ -433,7 +453,7 @@ export default function (mirageConfig) {
});

return new Response(200, {}, { hermesDocuments, externalLinks });
}
},
);

/**
Expand All @@ -454,7 +474,7 @@ export default function (mirageConfig) {
Hits: drafts,
params: "",
page: 0,
}
},
);
});

Expand Down Expand Up @@ -486,7 +506,7 @@ export default function (mirageConfig) {
return new Response(
200,
{},
{ "Default Fetched Product": { abbreviation: "NONE" } }
{ "Default Fetched Product": { abbreviation: "NONE" } },
);
} else {
let objects = this.schema.products.all().models.map((product) => {
Expand Down Expand Up @@ -559,7 +579,7 @@ export default function (mirageConfig) {
schema.db.recentlyViewedDocs.remove();
schema.db.recentlyViewedDocs.insert(index);
return new Response(200, {}, schema.recentlyViewedDocs.all().models);
}
},
);

/**
Expand Down Expand Up @@ -633,7 +653,7 @@ export default function (mirageConfig) {
type: mirageDocument.docType,
documentNumber: mirageDocument.docNumber,
});
}
},
);

externalLinks.forEach((link) => {
Expand All @@ -646,7 +666,7 @@ export default function (mirageConfig) {
});

return new Response(200, {}, {});
}
},
);

// Update whether a draft is shareable.
Expand Down
Loading