Skip to content

Commit 8a313d5

Browse files
steverydzbartaz
andcommitted
Update static/js/publisher/pages/RegisterSnap/__tests__/RegisterSnap.test.tsx
Co-authored-by: Bartek Szopka <83575+bartaz@users.noreply.github.com>
1 parent 19444bf commit 8a313d5

5 files changed

Lines changed: 135 additions & 95 deletions

File tree

static/js/publisher/pages/RegisterSnap/RegisterSnap.tsx

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ReactNode, useState } from "react";
22
import { Link } from "react-router-dom";
33
import { Notification } from "@canonical/react-components";
44

5+
import RegistrationError from "./RegistrationError";
56
import RegisterSnapForm from "./RegisterSnapForm";
67

78
import { filterAvailableStores } from "../../utils";
@@ -17,6 +18,7 @@ function RegisterSnap(): ReactNode {
1718
const { data, isLoading } = useBrandStores();
1819

1920
const availableStores = filterAvailableStores(data || []);
21+
const [selectedStore, setSelectedStore] = useState<string>("ubuntu");
2022

2123
return (
2224
<div className="u-fixed-width">
@@ -42,59 +44,28 @@ function RegisterSnap(): ReactNode {
4244
<h1 className="p-heading--2">Register snap</h1>
4345
)}
4446

45-
{registrationResponse?.error_code === "name-review-required" && (
46-
<Notification severity="positive">
47-
<strong>{registrationResponse?.snap_name}</strong> has been submitted
48-
for review
49-
</Notification>
50-
)}
51-
52-
{registrationResponse?.error_code === "already_owned" && (
53-
<Notification severity="caution">
54-
You already own '
55-
<Link to={`/${registrationResponse.snap_name}/listing`}>
56-
<strong>{registrationResponse.snap_name}</strong>
57-
</Link>
58-
'.
59-
</Notification>
60-
)}
61-
62-
{registrationResponse?.error_code === "reserved_name" && (
63-
<Notification severity="caution">
64-
'<strong>{registrationResponse.snap_name}</strong>' is reserved. You
65-
can{" "}
66-
<a
67-
href={`/request-reserved-name?snap_name=${registrationResponse.snap_name}&store=${registrationResponse.store}&is_private=${registrationResponse.is_private}`}
68-
>
69-
request a reserved name
70-
</a>{" "}
71-
or register a new name below.
72-
</Notification>
47+
{registrationResponse?.error_code && (
48+
<RegistrationError
49+
snapName={registrationResponse.snap_name}
50+
isPrivate={registrationResponse.is_private}
51+
store={registrationResponse.store}
52+
errorCode={registrationResponse.error_code}
53+
/>
7354
)}
7455

75-
{registrationResponse?.error_code === "no-permission" && (
76-
<Notification severity="caution">
77-
You do not have permission to register snaps to this store. Contact
78-
the store administrator.
56+
{selectedStore === "ubuntu" && (
57+
<Notification severity="information">
58+
Snap name registrations are subject to manual review. You will be able
59+
to upload your snap and update its metadata, but you will not be able
60+
to make the Snap public until the review has been completed. We aim to
61+
review all registrations within 30 days
7962
</Notification>
8063
)}
8164

82-
{registrationResponse?.error_code !== "already_registered" && (
83-
<p>
84-
Before you can push your snap to the store, its name must be
85-
registered
86-
</p>
87-
)}
88-
89-
<Notification severity="information">
90-
Snap name registrations are subject to manual review. You will be able
91-
to upload your snap and update its metadata, but you will not be able to
92-
make the Snap public until the review has been completed. We aim to
93-
review all registrations within 30 days
94-
</Notification>
95-
9665
{!isLoading && availableStores.length > 0 && (
9766
<RegisterSnapForm
67+
selectedStore={selectedStore}
68+
setSelectedStore={setSelectedStore}
9869
isSending={isSending}
9970
setIsSending={setIsSending}
10071
setRegistrationResponse={setRegistrationResponse}

static/js/publisher/pages/RegisterSnap/RegisterSnapForm.tsx

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { ReactNode, useState, Dispatch, SetStateAction } from "react";
1+
import {
2+
ReactNode,
3+
useState,
4+
Dispatch,
5+
SetStateAction,
6+
FormEvent,
7+
} from "react";
28
import { Link, useNavigate } from "react-router-dom";
39
import {
410
Form,
@@ -19,57 +25,60 @@ type Props = {
1925
SetStateAction<RegistrationResponse | undefined>
2026
>;
2127
availableStores: { name: string; id: string }[];
28+
selectedStore: string;
29+
setSelectedStore: Dispatch<SetStateAction<string>>;
2230
};
2331

2432
function RegisterSnapForm({
2533
isSending,
2634
setIsSending,
2735
setRegistrationResponse,
2836
availableStores,
37+
selectedStore,
38+
setSelectedStore,
2939
}: Props): ReactNode {
3040
const [snapName, setSnapName] = useState<string>();
31-
const [selectedStore, setSelectedStore] = useState<string>("ubuntu");
32-
const [isPrivate, setIsPrivate] = useState<string>("private");
41+
const [privacy, setPrivacy] = useState<string>("private");
3342

3443
const navigate = useNavigate();
3544

36-
return (
37-
<Form
38-
onSubmit={async (e) => {
39-
e.preventDefault();
45+
const handleSubmit = async (e: FormEvent) => {
46+
e.preventDefault();
4047

41-
setIsSending(true);
48+
setIsSending(true);
4249

43-
const formData = new FormData();
44-
formData.set("csrf_token", window.CSRF_TOKEN);
45-
formData.set("snap_name", snapName || "");
46-
formData.set("store", selectedStore);
50+
const formData = new FormData();
51+
formData.set("csrf_token", window.CSRF_TOKEN);
52+
formData.set("snap_name", snapName || "");
53+
formData.set("store", selectedStore);
4754

48-
const response = await fetch("/api/register-snap", {
49-
method: "POST",
50-
headers: {
51-
"X-CSRFToken": window.CSRF_TOKEN,
52-
},
53-
body: formData,
54-
});
55+
const response = await fetch("/api/register-snap", {
56+
method: "POST",
57+
headers: {
58+
"X-CSRFToken": window.CSRF_TOKEN,
59+
},
60+
body: formData,
61+
});
5562

56-
if (!response.ok) {
57-
setIsSending(false);
58-
throw new Error("Unable to register snap name");
59-
}
63+
if (!response.ok) {
64+
setIsSending(false);
65+
throw new Error("Unable to register snap name");
66+
}
6067

61-
const responseData = await response.json();
68+
const responseData = await response.json();
6269

63-
setTimeout(() => {
64-
if (responseData.success) {
65-
navigate("/snaps");
66-
} else {
67-
setRegistrationResponse(responseData.data);
68-
setIsSending(false);
69-
}
70-
}, 1000);
71-
}}
72-
>
70+
setTimeout(() => {
71+
if (responseData.success) {
72+
navigate("/snaps");
73+
} else {
74+
setRegistrationResponse(responseData.data);
75+
setIsSending(false);
76+
}
77+
}, 1000);
78+
};
79+
80+
return (
81+
<Form onSubmit={handleSubmit}>
7382
<Row>
7483
<Col size={8}>
7584
<Select
@@ -82,7 +91,7 @@ function RegisterSnapForm({
8291
setSelectedStore(e.target.value);
8392

8493
if (e.target.value === "ubuntu") {
85-
setIsPrivate("private");
94+
setPrivacy("private");
8695
}
8796
}}
8897
required
@@ -106,9 +115,9 @@ function RegisterSnapForm({
106115
name="public"
107116
disabled={selectedStore === "ubuntu"}
108117
value="public"
109-
checked={isPrivate === "public"}
118+
checked={privacy === "public"}
110119
onChange={(e) => {
111-
setIsPrivate(e.target.value);
120+
setPrivacy(e.target.value);
112121
}}
113122
/>
114123
<Input
@@ -118,9 +127,9 @@ function RegisterSnapForm({
118127
help="Snap is hidden in stores and only accessible by the publisher and collaborators"
119128
disabled={selectedStore === "ubuntu"}
120129
value="private"
121-
checked={isPrivate === "private" || selectedStore === "ubuntu"}
130+
checked={privacy === "private" || selectedStore === "ubuntu"}
122131
onChange={(e) => {
123-
setIsPrivate(e.target.value);
132+
setPrivacy(e.target.value);
124133
}}
125134
/>
126135
</Col>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Link } from "react-router-dom";
2+
import { Notification } from "@canonical/react-components";
3+
4+
type Props = {
5+
errorCode: string;
6+
snapName: string;
7+
isPrivate: string;
8+
store: string;
9+
};
10+
11+
function RegistrationError({ snapName, errorCode, isPrivate, store }: Props) {
12+
if (errorCode === "name-review-required") {
13+
return (
14+
<Notification severity="information">
15+
<strong>{snapName}</strong> has been submitted for review
16+
</Notification>
17+
);
18+
}
19+
20+
if (errorCode === "already_owned") {
21+
return (
22+
<Notification severity="information">
23+
You already own '
24+
<Link to={`/${snapName}/listing`}>
25+
<strong>{snapName}</strong>
26+
</Link>
27+
'.
28+
</Notification>
29+
);
30+
}
31+
32+
if (errorCode === "reserved_name") {
33+
return (
34+
<Notification severity="information">
35+
'<strong>{snapName}</strong>' is reserved. You can{" "}
36+
<a
37+
href={`/request-reserved-name?snap_name=${snapName}&store=${store}&is_isPrivate=${isPrivate}`}
38+
>
39+
request a reserved name
40+
</a>{" "}
41+
or register a new name below.
42+
</Notification>
43+
);
44+
}
45+
46+
if (errorCode === "no-permission") {
47+
return (
48+
<Notification severity="information">
49+
You do not have permission to register snaps to this store. Contact the
50+
store administrator.
51+
</Notification>
52+
);
53+
}
54+
55+
if (errorCode !== "already_registered") {
56+
return (
57+
<Notification severity="information">
58+
Before you can push your snap to the store, its name must be registered
59+
</Notification>
60+
);
61+
}
62+
}
63+
64+
export default RegistrationError;

static/js/publisher/pages/RegisterSnap/__tests__/RegisterSnap.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe("RegisterSnap", () => {
131131
});
132132
});
133133

134-
test("enabled public and private fields if not global store", async () => {
134+
test("enables public and private fields if not global store", async () => {
135135
const user = userEvent.setup();
136136

137137
server.use(

tests/publisher/tests_register_name.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ def test_register_name_logged_in(self):
2020
self.assert_template_used("store/publisher.html")
2121

2222

23-
class PostRegisterNamePageNotAuth(BaseTestCases.EndpointLoggedOut):
24-
def setUp(self):
25-
endpoint_url = "/api/register-snap"
26-
27-
super().setUp(
28-
snap_name=None, endpoint_url=endpoint_url, method_endpoint="POST"
29-
)
30-
31-
3223
class PostRegisterNamePage(BaseTestCases.EndpointLoggedIn):
3324
def setUp(self):
3425
endpoint_url = "/api/register-snap"
@@ -111,6 +102,9 @@ def test_name_already_registered(self):
111102
response = self.client.post(self.endpoint_url, data=self.data)
112103

113104
assert response.status_code == 200
105+
assert (
106+
response.get_json()["data"]["error_code"] == "already_registered"
107+
)
114108

115109
@responses.activate
116110
def test_name_reserved(self):
@@ -125,6 +119,7 @@ def test_name_reserved(self):
125119
response = self.client.post(self.endpoint_url, data=self.data)
126120

127121
assert response.status_code == 200
122+
assert response.get_json()["data"]["error_code"] == "reserved_name"
128123

129124
@responses.activate
130125
def test_claim_dispute(self):
@@ -142,3 +137,4 @@ def test_claim_dispute(self):
142137
response = self.client.post(self.endpoint_url, data=self.data)
143138

144139
assert response.status_code == 200
140+
assert response.get_json()["success"] is True

0 commit comments

Comments
 (0)