'Helper for common functionality in edit membership tests.
Checks for all relevant assertions about membership being removed,
including verify edx.team.learner_removed events are emitted.'

def edit_membership_helper(self, role, cancel=False):

    if (role is not None):
        AutoAuthPage(self.browser, course_id=self.course_id, staff=False, roles=role).visit()
    self.team_page.visit()
    self.team_page.click_edit_team_button()
    self.team_management_page.wait_for_page()
    self.assertTrue(self.team_management_page.membership_button_present)
    self.team_management_page.click_membership_button()
    self.edit_membership_page.wait_for_page()
    self.edit_membership_page.click_first_remove()
    if cancel:
        self.edit_membership_page.cancel_delete_membership_dialog()
        self.assertEqual(self.edit_membership_page.team_members, 1)
    else:
        expected_events = [{'event_type': 'edx.team.learner_removed', 'event': {'team_id': self.team['id'], 'remove_method': 'removed_by_admin', 'user_id': self.user_info['user_id']}}]
        with self.assert_events_match_during(event_filter=self.only_team_events, expected_events=expected_events):
            self.edit_membership_page.confirm_delete_membership_dialog()
        self.assertEqual(self.edit_membership_page.team_members, 0)
    self.edit_membership_page.wait_for_page()
